const fs = require('fs');
const path = require('path');

const root = path.join(__dirname, '..');
const styleDir = path.join(root, 'common/style/memberInfo');
const pagesDir = path.join(styleDir, 'pages');
const componentsDir = path.join(root, 'components/memberInfo');

const cssMap = {
    MemberInfoStatusBar: 'member-info-status-bar.css',
    MemberInfoHeader: 'member-info-header.css',
    MemberInfoMemberCard: 'member-info-member-card.css',
    MemberInfoQuickActions: 'member-info-quick-actions.css',
    MemberInfoBookingList: 'member-info-booking-list.css',
    MemberInfoCheckInList: 'member-info-check-in-list.css',
    MemberInfoBodyReport: 'member-info-body-report.css',
    MemberInfoCouponPoints: 'member-info-coupon-points.css',
    MemberInfoReferral: 'member-info-referral.css',
    MemberInfoSettings: 'member-info-settings.css',
    MemberInfoLogout: 'member-info-logout.css'
};

function stripCssImports(filePath) {
    let css = fs.readFileSync(filePath, 'utf8').replace(/\r\n/g, '\n');
    const next = css.replace(/^@import\s+[^;]+;\s*\n/gm, '');
    if (next !== css) {
        fs.writeFileSync(filePath, next.replace(/^\n+/, ''), 'utf8');
        console.log('stripped imports:', path.relative(root, filePath));
    }
}

// 纯样式文件：去掉嵌套 @import（小程序只认 vue 里第一层 @import）
for (const name of fs.readdirSync(styleDir)) {
    if (!name.endsWith('.css') || name === 'member-info-all.css') continue;
    stripCssImports(path.join(styleDir, name));
}
for (const name of fs.readdirSync(pagesDir)) {
    if (!name.endsWith('.css')) continue;
    stripCssImports(path.join(pagesDir, name));
}

const componentStyleBlock = (cssFile) => `<style>
@import '@/common/style/base.css';
@import '@/common/style/memberInfo/member-info-component-reset.css';
@import '@/common/style/memberInfo/${cssFile}';
</style>
`;

for (const [name, cssFile] of Object.entries(cssMap)) {
    const filePath = path.join(componentsDir, `${name}.vue`);
    let content = fs.readFileSync(filePath, 'utf8').replace(/\r\n/g, '\n');
    content = content.replace(/\n<style>[\s\S]*?<\/style>\n?/g, '\n');
    content = content.trimEnd() + '\n' + componentStyleBlock(cssFile);
    fs.writeFileSync(filePath, content, 'utf8');
    console.log('updated component:', name);
}

const memberInfoVue = path.join(root, 'pages/memberInfo/memberInfo.vue');
let memberInfoContent = fs.readFileSync(memberInfoVue, 'utf8').replace(/\r\n/g, '\n');
memberInfoContent = memberInfoContent.replace(
    /<style>[\s\S]*?<\/style>/,
    `<style>
@import '@/common/style/base.css';
@import '@/common/style/memberInfo/member-info-page.css';
</style>`
);
fs.writeFileSync(memberInfoVue, memberInfoContent, 'utf8');
console.log('updated memberInfo.vue');

const subPages = {
    booking: ['booking-page.css', 'booking-pixso.css'],
    memberCard: ['member-card-page.css', 'member-card-pixso.css'],
    userInfo: ['user-info-page.css', 'user-info-pixso.css']
};

for (const [page, files] of Object.entries(subPages)) {
    const vuePath = path.join(root, `pages/memberInfo/${page}.vue`);
    let content = fs.readFileSync(vuePath, 'utf8').replace(/\r\n/g, '\n');
    const imports = [
        "@import '@/common/style/base.css';",
        "@import '@/common/style/memberInfo/pages/page-reset.css';",
        "@import '@/common/style/memberInfo/pages/sub-page-base.css';",
        ...files.map((f) => `@import '@/common/style/memberInfo/pages/${f}';`)
    ].join('\n');
    content = content.replace(/<style>[\s\S]*?<\/style>/, `<style>\n${imports}\n</style>`);
    fs.writeFileSync(vuePath, content, 'utf8');
    console.log('updated', `${page}.vue`);
}

console.log('done');
