import { NextRequest } from 'next/server';

export async function POST(req: NextRequest) {
    try {
        const { searchParams } = new URL(req.url);

        const body = await req.json();
        const { symbol, clicked_date } = body;
        if (!symbol || !clicked_date) {
            return new Response(JSON.stringify({ error: 'Missing required parameters: symbol and clicked_date' }), {
                status: 400,
                headers: { 'Content-Type': 'application/json' },
            });
        }


        // === 3. POST to Flask backend ===
        const predictResponse = await fetch('http://localhost:8000/predict', {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json',
                'Accept': 'application/json',
            },
            body: JSON.stringify({
                symbol,
                clicked_date
            }),
        });

        if (!predictResponse.ok) {
            const errorData = await predictResponse.json();
            return new Response(JSON.stringify({ error: errorData.error || 'Failed to get predictions' }), {
                status: predictResponse.status,
                headers: { 'Content-Type': 'application/json' },
            });
        }

        const predictionData = await predictResponse.json();
        console.log('Prediction Data:', predictionData);

        return new Response(JSON.stringify(predictionData), {
            status: 200,
            headers: { 'Content-Type': 'application/json' },
        });

    } catch (err: any) {
        console.error('Server Error:', err);
        return new Response(JSON.stringify({ error: err.message || 'Internal server error' }), {
            status: 500,
            headers: { 'Content-Type': 'application/json' },
        });
    }
}
