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

export async function seedCompanies() {
    console.log('🏢 Seeding companies...');

    // Get region IDs
    const regions = await pool.query('SELECT id, code FROM regions');
    const regMap = {};
    regions.rows.forEach(r => regMap[r.code] = r.id);

    // Fallback ID (Tokyo or first available)
    const defaultRegionId = regMap['13'] || (regions.rows.length > 0 ? regions.rows[0].id : null);

    const companies = [
        {
            name: 'TechCorp',
            slug: 'techcorp',
            type: 'company',
            document: '12.345.678/0001-90',
            address: 'Av. Paulista, 1000, São Paulo - SP',
            regionId: regMap['SP'] || defaultRegionId,
            phone: '+55-11-3000-0000',
            email: 'contact@techcorp.com.br',
            website: 'https://techcorp.com.br',
            description: JSON.stringify('Leading software development company specializing in enterprise solutions.'),
            verified: true
        },
        {
            name: 'DesignHub',
            slug: 'designhub',
            type: 'company',
            document: '98.765.432/0001-12',
            address: 'Rua Augusta, 500, São Paulo - SP',
            regionId: regMap['SP'] || defaultRegionId,
            phone: '+55-11-3000-1111',
            email: 'hello@designhub.studio',
            website: 'https://designhub.studio',
            description: JSON.stringify('Creative agency focused on UI/UX and branding.'),
            verified: true
        },
        {
            name: 'DataFlow',
            slug: 'dataflow',
            type: 'company',
            document: '11.223.344/0001-56',
            address: 'Av. Atlântica, 200, Rio de Janeiro - RJ',
            regionId: regMap['RJ'] || defaultRegionId,
            phone: '+55-21-2000-2222',
            email: 'jobs@dataflow.io',
            website: 'https://dataflow.io',
            description: JSON.stringify('Big Data and Analytics solutions for modern businesses.'),
            verified: true
        },
        {
            name: 'InnovateLab',
            slug: 'innovatelab',
            type: 'company',
            document: '55.667.788/0001-99',
            address: 'Praça da Liberdade, 50, Belo Horizonte - MG',
            regionId: regMap['MG'] || defaultRegionId,
            phone: '+55-31-3000-3333',
            email: 'careers@innovatelab.mg',
            website: 'https://innovatelab.mg',
            description: JSON.stringify('Innovation hub for product development and startups.'),
            verified: true
        },
        {
            name: 'AppMakers',
            slug: 'appmakers',
            type: 'company',
            document: '33.445.566/0001-77',
            address: 'Vila Madalena, São Paulo - SP',
            regionId: regMap['SP'] || defaultRegionId,
            phone: '+55-11-3000-4444',
            email: 'dev@appmakers.mobile',
            website: 'https://appmakers.mobile',
            description: JSON.stringify('Mobile app development specialists.'),
            verified: true
        },
        {
            name: 'CloudTech',
            slug: 'cloudtech',
            type: 'company',
            document: '77.889.900/0001-22',
            address: 'Berrini, São Paulo - SP',
            regionId: regMap['SP'] || defaultRegionId,
            phone: '+55-11-3000-5555',
            email: 'ops@cloudtech.infra',
            website: 'https://cloudtech.infra',
            description: JSON.stringify('Cloud infrastructure and DevOps consultancy.'),
            verified: true
        }
    ];

    try {
        for (const company of companies) {
            await pool.query(`
        INSERT INTO companies (name, slug, type, document, address, region_id, phone, email, website, description, verified, active)
        VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12)
        ON CONFLICT (slug) DO UPDATE SET
            name = EXCLUDED.name,
            type = EXCLUDED.type,
            document = EXCLUDED.document,
            address = EXCLUDED.address,
            region_id = EXCLUDED.region_id,
            phone = EXCLUDED.phone,
            email = EXCLUDED.email,
            website = EXCLUDED.website,
            description = EXCLUDED.description,
            verified = EXCLUDED.verified,
            active = EXCLUDED.active
      `, [
                company.name,
                company.slug,
                company.type,
                company.document,
                company.address,
                company.regionId,
                company.phone,
                company.email,
                company.website,
                company.description,
                company.verified,
                true
            ]);

            // Seed Core Company (One-to-one mapping for now)
            const coreId = crypto.randomUUID();
            await pool.query(`
                INSERT INTO core_companies (id, name, document, status)
                VALUES ($1, $2, $3, 'ACTIVE')
                ON CONFLICT (id) DO NOTHING -- UUID collision unlikely, but good practice
            `, [coreId, company.name, company.document]);
        }

        // Seed System Tenant for SuperAdmin
        await pool.query(`
            INSERT INTO core_companies (id, name, document, status)
            VALUES ('00000000-0000-0000-0000-000000000000', 'GoHorse System', '00.000.000/0001-91', 'ACTIVE')
            ON CONFLICT (id) DO NOTHING
        `);

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