import * as fs from 'fs';
import * as path from 'path';

// Using exact string parsing since importing the TS files directly in tsx could have issues if the environment isn't fully set up, but tsx should work. Let's just import them.
import { LEXICON_BATCH_1_ENTRIES } from '../constants/lexiconBatch1';
import { LEXICON_BATCH_2_ENTRIES } from '../constants/lexiconBatch2';

const allPlants = [...LEXICON_BATCH_1_ENTRIES, ...LEXICON_BATCH_2_ENTRIES];

async function checkUrl(url: string): Promise<boolean> {
    const headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36',
        'Accept': 'image/avif,image/webp,image/apng,image/svg+xml,image/*,*/*;q=0.8',
        'Referer': 'https://commons.wikimedia.org/'
    };
    try {
        const response = await fetch(url, { method: 'GET', headers });
        return response.status === 200;
    } catch (error) {
        return false;
    }
}

async function run() {
    console.log(`Checking ${allPlants.length} plants...`);
    let failedCount = 0;
    const concurrency = 10;

    for (let i = 0; i < allPlants.length; i += concurrency) {
        const batch = allPlants.slice(i, i + concurrency);
        const results = await Promise.all(batch.map(async p => {
            const ok = await checkUrl(p.imageUri);
            return {
                name: p.name,
                url: p.imageUri,
                ok
            };
        }));

        for (const res of results) {
            if (!res.ok) {
                console.log(`❌ Failed: ${res.name} -> ${res.url}`);
                failedCount++;
            }
        }
    }

    if (failedCount === 0) {
        console.log("✅ All image URLs are reachable!");
    } else {
        console.log(`❌ ${failedCount} URLs failed.`);
    }
}

run();
