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

const updateEnum = async () => {
    try {
        console.log('>>> STARTING ENUM MIGRATION <<<');
        await db.sequelize.authenticate();
        console.log('Database connection established.');

        // Raw query to add values to enum
        // Note: PostgreSQL cannot remove enum values, only add.
        // We will add the "Interview Pending" variations.

        const queryInterface = db.sequelize.getQueryInterface();

        try {
            await db.sequelize.query(`ALTER TYPE "enum_applications_overallStatus" ADD VALUE IF NOT EXISTS 'Level 1 Interview Pending';`);
            console.log('Added Level 1 Interview Pending');
        } catch (e) { console.log('Level 1 Interview Pending likely exists or error', e.message); }

        try {
            await db.sequelize.query(`ALTER TYPE "enum_applications_overallStatus" ADD VALUE IF NOT EXISTS 'Level 2 Interview Pending';`);
            console.log('Added Level 2 Interview Pending');
        } catch (e) { console.log('Level 2 Interview Pending likely exists or error', e.message); }

        try {
            await db.sequelize.query(`ALTER TYPE "enum_applications_overallStatus" ADD VALUE IF NOT EXISTS 'Level 3 Interview Pending';`);
            console.log('Added Level 3 Interview Pending');
        } catch (e) { console.log('Level 3 Interview Pending likely exists or error', e.message); }

        console.log('>>> SUCCESS: Enum values updated <<<');

        await db.sequelize.close();
        process.exit(0);
    } catch (error) {
        console.error('>>> ERROR: Failed to update Enum', error);
        process.exit(1);
    }
};

updateEnum();
