import { NextResponse } from 'next/server';

export async function POST(request: Request) {
    try {
        const body = await request.json();

        const response = await fetch('https://llm.thedomainnest.com/chat-json', {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json',
            },
            body: JSON.stringify(body),
        });

        if (!response.ok) {
            const errorText = await response.text();
            console.error('LLM Proxy External Error:', response.status, errorText);
            return NextResponse.json({
                ok: false,
                error: `External API error (${response.status}). The service might be overloaded or the mode "${body.mode}" is not supported.`
            }, { status: response.status });
        }

        const data = await response.json();
        return NextResponse.json(data);
    } catch (error: any) {
        console.error('LLM Proxy Exception:', error);
        return NextResponse.json({ ok: false, error: error.message }, { status: 500 });
    }
}
