import { NextResponse } from 'next/server';
import { getPrompts, syncPromptsService } from '@/lib/prompts-service';

export async function GET() {
    try {
        const cache = await getPrompts();

        // Lazy Auto-Crawl: Check if sync is needed (every 1 hour)
        const ONE_HOUR = 60 * 60 * 1000;
        const lastSync = cache.lastSync || 0;

        if (Date.now() - lastSync > ONE_HOUR) {
            console.log("[Auto-Crawl] Triggering background sync...");
            // Fire and forget - don't await to keep UI fast
            syncPromptsService().catch(err => console.error("[Auto-Crawl] Failed:", err));
        }

        return NextResponse.json(cache);
    } catch (error) {
        return NextResponse.json({ error: 'Failed to load prompts' }, { status: 500 });
    }
}
