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

const seedSystemConfigs = async () => {
    try {
        console.log('🚀 Starting System Configuration Seeding...');
        
        // Test connection
        console.log('📡 Testing database connection...');
        await db.sequelize.authenticate();
        console.log('✅ Database connection established.');

        const configs = [
            {
                key: 'SECURITY_DEPOSIT',
                value: { amount: 500000, currency: 'INR' },
                category: 'SECURITY_DEPOSIT',
                description: 'Default Security Deposit amount for new dealer onboarding'
            },
            {
                key: 'FIRST_FILL',
                value: { amount: 1500000, currency: 'INR' },
                category: 'SECURITY_DEPOSIT',
                description: 'Default First Fill amount for new dealer onboarding'
            },
            {
                key: 'AUTO_ASSIGN_ONBOARDING',
                value: { enabled: true },
                category: 'ASSIGNMENT',
                description: 'Enable/Disable auto-assignment of participants for Onboarding module'
            },
            {
                key: 'AUTO_ASSIGN_RELOCATION',
                value: { enabled: true },
                category: 'ASSIGNMENT',
                description: 'Enable/Disable auto-assignment of participants for Relocation module'
            },
            {
                key: 'AUTO_ASSIGN_TERMINATION',
                value: { enabled: true },
                category: 'ASSIGNMENT',
                description: 'Enable/Disable auto-assignment of participants for Termination module'
            },
            {
                key: 'AUTO_ASSIGN_CONSTITUTIONAL',
                value: { enabled: true },
                category: 'ASSIGNMENT',
                description: 'Enable/Disable auto-assignment of participants for Constitutional module'
            },
            {
                key: 'AUTO_ASSIGN_RESIGNATION',
                value: { enabled: true },
                category: 'ASSIGNMENT',
                description: 'Enable/Disable auto-assignment of participants for Resignation module'
            },
            {
                key: 'AUTO_ASSIGN_FNF',
                value: { enabled: true },
                category: 'ASSIGNMENT',
                description: 'Enable/Disable auto-assignment of participants for F&F module'
            }
        ];

        console.log(`🌱 Seeding ${configs.length} configurations...`);

        for (const config of configs) {
            const [record, created] = await db.SystemConfiguration.findOrCreate({
                where: { key: config.key },
                defaults: {
                    ...config,
                    isActive: true
                }
            });

            if (created) {
                console.log(`✅ Created config: ${config.key}`);
            } else {
                console.log(`ℹ️ Config already exists: ${config.key}`);
            }
        }

        console.log('✨ System configurations seeded successfully.');
    } catch (error) {
        console.error('❌ Error during seeding:', error);
        throw error;
    }
};

// Execute seeding with proper error handling
seedSystemConfigs()
    .then(() => {
        console.log('👋 Seeding process completed.');
        process.exit(0);
    })
    .catch((err) => {
        console.error('💥 Fatal error:', err);
        process.exit(1);
    });

export default seedSystemConfigs;
