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

const componentsDir = path.join(__dirname, '../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'
};

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');

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

    if (!content.trimEnd().endsWith('</script>')) {
        console.warn('skip unexpected format:', name);
        continue;
    }

    content = content.trimEnd() + styleBlock;
    fs.writeFileSync(filePath, content, 'utf8');
    console.log('restored styles:', name);
}

const pageFiles = [
    path.join(__dirname, '../pages/memberInfo/memberInfo.vue'),
    path.join(__dirname, '../pages/memberInfo/userInfo.vue')
];

for (const pagePath of pageFiles) {
    let content = fs.readFileSync(pagePath, 'utf8');
    content = content.replace(
        "@import '@/common/style/member-info-all.css';",
        "@import '@/common/style/memberInfo/member-info-page.css';"
    );
    content = content.replace(
        "@import '@/common/style/member-info-page.css';",
        "@import '@/common/style/memberInfo/member-info-page.css';"
    );
    fs.writeFileSync(pagePath, content, 'utf8');
    console.log('updated page:', path.basename(pagePath));
}

console.log('done');
