import pg from 'pg';
const { Client } = pg;

const client = new Client({
    user: 'laxman',
    host: 'localhost',
    database: 'royal_enfield_onboarding',
    password: 'Admin@123',
    port: 5432,
});

async function debugOpportunity() {
    try {
        await client.connect();

        const districtName = 'Bijapur';
        console.log(`Checking for District: ${districtName}`);

        // Note: ILIKE is Postgres specific, checking matches
        const districtRes = await client.query(`SELECT * FROM districts WHERE "districtName" ILIKE $1`, [`%${districtName}%`]);

        if (districtRes.rows.length === 0) {
            console.log('No district found matching "Bijapur"');
        } else {
            console.log(`Found ${districtRes.rows.length} districts:`);
            for (const d of districtRes.rows) {
                console.log(`- ID: ${d.id}, Name: ${d.districtName}, StateID: ${d.stateId}`);

                // Check opportunities
                const oppRes = await client.query(`SELECT * FROM opportunities WHERE "districtId" = $1`, [d.id]);
                if (oppRes.rows.length === 0) {
                    console.log(`  -> No opportunities found for this district.`);
                } else {
                    for (const o of oppRes.rows) {
                        console.log(`  -> Opportunity Found: ID=${o.id}, Status=${o.status}, Type=${o.opportunityType}`);
                    }
                }
            }
        }

    } catch (err) {
        console.error('Error:', err);
    } finally {
        await client.end();
        process.exit();
    }
}

debugOpportunity();
