import { NextResponse } from 'next/server';
export const dynamic = 'force-dynamic';

const POPULAR_FONTS = [
    { name: "Inter", value: "Inter", category: "sans-serif" },
    { name: "Roboto", value: "Roboto", category: "sans-serif" },
    { name: "Open Sans", value: "Open Sans", category: "sans-serif" },
    { name: "Lato", value: "Lato", category: "sans-serif" },
    { name: "Montserrat", value: "Montserrat", category: "sans-serif" },
    { name: "Oswald", value: "Oswald", category: "sans-serif" },
    { name: "Raleway", value: "Raleway", category: "sans-serif" },
    { name: "Playfair Display", value: "Playfair Display", category: "serif" },
    { name: "Merriweather", value: "Merriweather", category: "serif" },
    { name: "Nunito", value: "Nunito", category: "sans-serif" },
    { name: "Dancing Script", value: "Dancing Script", category: "handwriting" },
    { name: "Pacifico", value: "Pacifico", category: "handwriting" },
    { name: "Poppins", value: "Poppins", category: "sans-serif" },
    { name: "Source Sans Pro", value: "Source Sans Pro", category: "sans-serif" },
    { name: "Ubuntu", value: "Ubuntu", category: "sans-serif" },
    { name: "Rubik", value: "Rubik", category: "sans-serif" },
    { name: "Work Sans", value: "Work Sans", category: "sans-serif" },
    { name: "Quicksand", value: "Quicksand", category: "sans-serif" },
    { name: "Josefin Sans", value: "Josefin Sans", category: "sans-serif" },
    { name: "Libre Baskerville", value: "Libre Baskerville", category: "serif" },
    { name: "Crimson Text", value: "Crimson Text", category: "serif" },
    { name: "Bitter", value: "Bitter", category: "serif" },
    { name: "Archivo", value: "Archivo", category: "sans-serif" },
    { name: "DM Sans", value: "DM Sans", category: "sans-serif" },
    { name: "Space Grotesk", value: "Space Grotesk", category: "sans-serif" },
    { name: "Outfit", value: "Outfit", category: "sans-serif" },
    { name: "Sora", value: "Sora", category: "sans-serif" },
    { name: "Caveat", value: "Caveat", category: "handwriting" },
    { name: "Comfortaa", value: "Comfortaa", category: "display" },
    { name: "Barlow", value: "Barlow", category: "sans-serif" },
    { name: "Karla", value: "Karla", category: "sans-serif" },
    { name: "Manrope", value: "Manrope", category: "sans-serif" },
    { name: "Lexend", value: "Lexend", category: "sans-serif" },
    { name: "Roboto Slab", value: "Roboto Slab", category: "serif" },
    { name: "PT Serif", value: "PT Serif", category: "serif" },
    { name: "Noto Sans", value: "Noto Sans", category: "sans-serif" },
    { name: "Fira Sans", value: "Fira Sans", category: "sans-serif" },
    { name: "IBM Plex Sans", value: "IBM Plex Sans", category: "sans-serif" },
    { name: "Cabin", value: "Cabin", category: "sans-serif" },
    { name: "Inconsolata", value: "Inconsolata", category: "monospace" },
];

let cachedGoogleFonts: any[] | null = null;
let cacheTimestamp = 0;
const CACHE_TTL = 24 * 60 * 60 * 1000; // 24 hours

export async function GET(request: Request) {
    const { searchParams } = new URL(request.url);
    const query = searchParams.get('q') || '';
    const apiKey = process.env.GOOGLE_FONTS_API_KEY;

    let fonts = POPULAR_FONTS;

    // Try Google Fonts API if key is configured
    if (apiKey && (!cachedGoogleFonts || Date.now() - cacheTimestamp > CACHE_TTL)) {
        try {
            const res = await fetch(
                `https://www.googleapis.com/webfonts/v1/webfonts?key=${apiKey}&sort=popularity`,
                { signal: AbortSignal.timeout(5000) }
            );
            if (res.ok) {
                const data = await res.json();
                cachedGoogleFonts = (data.items || []).map((f: any) => ({
                    name: f.family,
                    value: f.family,
                    category: f.category,
                }));
                cacheTimestamp = Date.now();
            }
        } catch {
            // Keep fallback
        }
    }

    if (cachedGoogleFonts) {
        fonts = cachedGoogleFonts;
    }

    if (query) {
        fonts = fonts.filter((f) =>
            f.name.toLowerCase().includes(query.toLowerCase())
        );
    }

    return NextResponse.json({ fonts: fonts.slice(0, 100) });
}
