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

export async function seedCities() {
    console.log('🏙️  Seeding global cities...');

    // Get region IDs
    const spResult = await pool.query("SELECT id FROM regions WHERE code = 'SP'");
    const caResult = await pool.query("SELECT id FROM regions WHERE code = 'CA'");
    const tokyoResult = await pool.query("SELECT id FROM regions WHERE code = '13'");

    if (spResult.rows.length === 0) {
        throw new Error('Regions must be seeded first');
    }

    const spId = spResult.rows[0].id;
    const caId = caResult.rows.length > 0 ? caResult.rows[0].id : null;
    const tokyoId = tokyoResult.rows.length > 0 ? tokyoResult.rows[0].id : null;

    const cities = [
        // SP
        { regionId: spId, name: 'São Paulo' },
        { regionId: spId, name: 'Campinas' },
        { regionId: spId, name: 'Santos' },
        // CA
        { regionId: caId, name: 'San Francisco' },
        { regionId: caId, name: 'Los Angeles' },
        { regionId: caId, name: 'Palo Alto' },
        // Tokyo
        { regionId: tokyoId, name: 'Shinjuku' },
        { regionId: tokyoId, name: 'Shibuya' }
    ];

    try {
        for (const city of cities) {
            if (!city.regionId) continue;

            await pool.query(`
        INSERT INTO cities (region_id, name)
        VALUES ($1, $2)
      `, [city.regionId, city.name]);
        }

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