import { NextResponse } from 'next/server';

export async function POST(req) {
    try {
        const body = await req.json();

        const response = await fetch(`${process.env.NEXT_PUBLIC_BACKEND_URL}/api/consultations/`, {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json',
                cookie: req.headers.get("cookie") || "",
            },
            body: JSON.stringify(body),
        });
        console.log(body);
        if (!response.ok) {
            console.error('Backend error:', await response.text());
            return new Response(JSON.stringify({ error: 'Failed to create consultation' }), { status: 500 });
        }

        const data = await response.json();
        return new Response(JSON.stringify(data), { status: 200 });

    } catch (error) {
        console.error('Error fetching consultations:', error);
        return new Response(JSON.stringify({ error: 'Internal server error' }), { status: 500 });
    }
}


export async function GET(req, res) {

    try {
        const response = await fetch(`${process.env.NEXT_PUBLIC_BACKEND_URL}/api/consultations/`, {
            headers: {
                'Content-Type': 'application/json',
                cookie: req.headers.get("cookie") || "",
            },
            method: 'GET',
        });

        if (!response.ok) {
            throw new Error('Failed to fetch consultations');
        }

        const data = await response.json();
        return new Response(JSON.stringify(data), { status: 200 });
    } catch (error) {
        console.error('Error fetching consultations:', error);
        return new Response(JSON.stringify({ error: 'Internal server error' }), { status: 500 });
    }
}