
import { QueryInterface, DataTypes } from 'sequelize';

export async function up(queryInterface: QueryInterface): Promise<void> {
    try {
        const tableDescription = await queryInterface.describeTable('workflow_templates');

        // 1. Rename id -> template_id
        if (tableDescription.id && !tableDescription.template_id) {
            console.log('Renaming id to template_id...');
            await queryInterface.renameColumn('workflow_templates', 'id', 'template_id');
        }

        // 2. Rename name -> template_name
        if (tableDescription.name && !tableDescription.template_name) {
            console.log('Renaming name to template_name...');
            await queryInterface.renameColumn('workflow_templates', 'name', 'template_name');
        }

        // 3. Rename description -> template_description
        if (tableDescription.description && !tableDescription.template_description) {
            console.log('Renaming description to template_description...');
            await queryInterface.renameColumn('workflow_templates', 'description', 'template_description');
        }

        // 4. Rename category -> template_category
        if (tableDescription.category && !tableDescription.template_category) {
            console.log('Renaming category to template_category...');
            await queryInterface.renameColumn('workflow_templates', 'category', 'template_category');
        }

        // 5. Rename suggested_sla -> default_tat_hours
        if (tableDescription.suggested_sla && !tableDescription.default_tat_hours) {
            console.log('Renaming suggested_sla to default_tat_hours...');
            await queryInterface.renameColumn('workflow_templates', 'suggested_sla', 'default_tat_hours');
        }

        // 6. Add missing columns
        if (!tableDescription.template_code) {
            console.log('Adding template_code column...');
            await queryInterface.addColumn('workflow_templates', 'template_code', {
                type: DataTypes.STRING(50),
                allowNull: true,
                unique: true
            });
        }

        if (!tableDescription.workflow_type) {
            console.log('Adding workflow_type column...');
            await queryInterface.addColumn('workflow_templates', 'workflow_type', {
                type: DataTypes.STRING(50),
                allowNull: true
            });
        }

        if (!tableDescription.approval_levels_config) {
            console.log('Adding approval_levels_config column...');
            await queryInterface.addColumn('workflow_templates', 'approval_levels_config', {
                type: DataTypes.JSONB,
                allowNull: true
            });
        }

        if (!tableDescription.form_steps_config) {
            console.log('Adding form_steps_config column...');
            await queryInterface.addColumn('workflow_templates', 'form_steps_config', {
                type: DataTypes.JSONB,
                allowNull: true
            });
        }

        if (!tableDescription.user_field_mappings) {
            console.log('Adding user_field_mappings column...');
            await queryInterface.addColumn('workflow_templates', 'user_field_mappings', {
                type: DataTypes.JSONB,
                allowNull: true
            });
        }

        if (!tableDescription.dynamic_approver_config) {
            console.log('Adding dynamic_approver_config column...');
            await queryInterface.addColumn('workflow_templates', 'dynamic_approver_config', {
                type: DataTypes.JSONB,
                allowNull: true
            });
        }

        if (!tableDescription.is_system_template) {
            console.log('Adding is_system_template column...');
            await queryInterface.addColumn('workflow_templates', 'is_system_template', {
                type: DataTypes.BOOLEAN,
                allowNull: false,
                defaultValue: false
            });
        }

        if (!tableDescription.usage_count) {
            console.log('Adding usage_count column...');
            await queryInterface.addColumn('workflow_templates', 'usage_count', {
                type: DataTypes.INTEGER,
                allowNull: false,
                defaultValue: 0
            });
        }

        console.log('✅ Schema validation/fix complete');
    } catch (error) {
        console.error('Error in schema fix migration:', error);
        throw error;
    }
}

export async function down(queryInterface: QueryInterface): Promise<void> {
    // Revert is complex/risky effectively, skipping for this fix-forward migration
}
