/**
 * 同步职位类型表结构
 * 创建 job_types 表并添加 job_type_id 字段到 pla_account 表
 */

const Framework = require('../framework/node-core-framework.js');
const frameworkConfig = require('../config/framework.config.js');

async function syncJobTypes() {
    try {
        console.log('[同步职位类型] 开始同步...');

        // 初始化框架
        await Framework.init(frameworkConfig);

        const models = Framework.getModels();
        const { job_types, pla_account } = models;

        if (!job_types) {
            throw new Error('无法获取 job_types 模型');
        }

        if (!pla_account) {
            throw new Error('无法获取 pla_account 模型');
        }

        // 从模型获取 sequelize 实例
        const sequelize = job_types.sequelize;
        if (!sequelize) {
            throw new Error('无法获取 sequelize 实例');
        }

        // 1. 同步 job_types 表
        console.log('[同步职位类型] 同步 job_types 表...');
        await job_types.sync({ alter: true });
        console.log('[同步职位类型] job_types 表同步完成');

        // 2. 同步 pla_account 表（会自动添加 job_type_id 字段）
        console.log('[同步职位类型] 同步 pla_account 表...');
        await pla_account.sync({ alter: true });
        console.log('[同步职位类型] pla_account 表同步完成');

        // 3. 检查 pla_account 表是否有 job_type_id 字段
        console.log('[同步职位类型] 检查 pla_account 表结构...');
        const Sequelize = require('sequelize');
        const tableInfo = await sequelize.query("DESCRIBE pla_account", { type: Sequelize.QueryTypes.SELECT });
        const hasJobTypeId = Array.isArray(tableInfo) && tableInfo.some(field => field.Field === 'job_type_id');

        if (hasJobTypeId) {
            console.log('[同步职位类型] ✅ pla_account 表已存在 job_type_id 字段');
        } else {
            console.log('[同步职位类型] ⚠️  pla_account 表未找到 job_type_id 字段，可能需要手动添加');
        }

        // 4. 显示表结构
        console.log('\n[同步职位类型] job_types 表结构:');
        const jobTypesInfo = await sequelize.query("DESCRIBE job_types", { type: Sequelize.QueryTypes.SELECT });
        if (Array.isArray(jobTypesInfo)) {
            console.table(jobTypesInfo.map(field => ({
                字段名: field.Field,
                类型: field.Type,
                允许空: field.Null,
                键: field.Key,
                默认值: field.Default,
                注释: field.Comment || ''
            })));
        }

        console.log('\n[同步职位类型] pla_account 表 job_type_id 字段:');
        const plaAccountInfo = await sequelize.query("DESCRIBE pla_account", { type: Sequelize.QueryTypes.SELECT });
        if (Array.isArray(plaAccountInfo)) {
            const jobTypeIdField = plaAccountInfo.find(field => field.Field === 'job_type_id');
            if (jobTypeIdField) {
                console.table([{
                    字段名: jobTypeIdField.Field,
                    类型: jobTypeIdField.Type,
                    允许空: jobTypeIdField.Null,
                    键: jobTypeIdField.Key,
                    默认值: jobTypeIdField.Default,
                    注释: jobTypeIdField.Comment || ''
                }]);
            } else {
                console.log('  ⚠️  未找到 job_type_id 字段');
            }
        }

        console.log('\n[同步职位类型] 同步完成！');
        process.exit(0);
    } catch (error) {
        console.error('[同步职位类型] 同步失败:', error);
        process.exit(1);
    }
}

// 执行同步
syncJobTypes();

