import { APPLICATION_STATUS } from '../src/common/config/constants.js';
import db from '../src/database/models/index.js';

async function updateEnum() {
    try {
        console.log('🔄 Syncing all APPLICATION_STATUS values with DB Enum...');
        
        const statuses = Object.values(APPLICATION_STATUS);
        
        for (const status of statuses) {
            try {
                // Posgres doesn't support IF NOT EXISTS for ADD VALUE in 9.5 and below
                // so we do it one by one and ignore "already exists" errors.
                await db.sequelize.query(`ALTER TYPE "enum_applications_overallStatus" ADD VALUE '${status}'`);
                console.log(`✅ Added: ${status}`);
            } catch (e: any) {
                if (e.message.includes('already exists')) {
                    // console.log(`ℹ️ Already exists: ${status}`);
                } else {
                    console.error(`❌ Error adding ${status}:`, e.message);
                }
            }
        }
        
        console.log('\n✅ Database Enum successfully synchronized with constants.');
    } catch (error: any) {
        console.error('❌ Critical failure during sync:', error.message);
    } finally {
        process.exit(0);
    }
}

updateEnum();
