import db from '../src/database/models/index.js';

async function migrate() {
    const queryInterface = db.sequelize.getQueryInterface();
    
    // Using describeTable to check existence
    const tableDefinition = await queryInterface.describeTable('constitutional_changes');

    console.log('--- Migrating constitutional_changes table ---');

    if (!tableDefinition.currentConstitution) {
        console.log('Adding currentConstitution column...');
        await queryInterface.addColumn('constitutional_changes', 'currentConstitution', {
            type: db.Sequelize.DataTypes.STRING,
            allowNull: true
        });
    }

    if (!tableDefinition.metadata) {
        console.log('Adding metadata column...');
        await queryInterface.addColumn('constitutional_changes', 'metadata', {
            type: db.Sequelize.DataTypes.JSON,
            defaultValue: {}
        });
    }

    // Update outletId to be nullable
    console.log('Updating outletId to be nullable...');
    await queryInterface.changeColumn('constitutional_changes', 'outletId', {
        type: db.Sequelize.DataTypes.UUID,
        allowNull: true
    });

    console.log('✅ Migration complete!');
}

migrate();
