import { pool } from '../db.js';

export async function seedApplications() {
    console.log('📝 Seeding applications...');

    // Get jobs
    const jobsRes = await pool.query('SELECT id FROM jobs LIMIT 5');
    const jobs = jobsRes.rows;

    // Get users from legacy users table (NOT core_users, since applications.user_id references users)
    const seekersRes = await pool.query("SELECT id FROM users LIMIT 10");
    const seekers = seekersRes.rows;

    if (jobs.length === 0) {
        console.log('   ⚠️  No jobs found, skipping applications');
        return;
    }

    if (seekers.length === 0) {
        console.log('   ⚠️  No users found, skipping applications');
        return;
    }

    const applications = [
        {
            jobId: jobs[0]?.id,
            userId: seekers[0]?.id,
            message: 'I have 2 years of experience in software development. Can start immediately.',
            resume_url: 'https://cdn.gohorsejobs.com/docs/resume_001.pdf',
            status: 'pending'
        },
        {
            jobId: jobs[1]?.id || jobs[0]?.id,
            userId: seekers[0]?.id,
            message: 'Interested in the designer position. Available for flexible hours.',
            resume_url: null,
            status: 'pending'
        },
        {
            jobId: jobs[0]?.id,
            userId: seekers[0]?.id,
            message: 'Hard worker with development experience.',
            resume_url: 'https://cdn.gohorsejobs.com/docs/resume_002.pdf',
            status: 'reviewed'
        }
    ];

    try {
        for (const app of applications) {
            if (!app.jobId || !app.userId) continue;
            await pool.query(`
                INSERT INTO applications (job_id, user_id, message, resume_url, status)
                VALUES ($1, $2, $3, $4, $5)
            `, [
                app.jobId, app.userId, app.message, app.resume_url, app.status
            ]);
        }

        console.log(`   ✓ ${applications.length} applications seeded (sample)`);
    } catch (error) {
        console.error('   ❌ Error seeding applications:', error.message);
        throw error;
    }
}

