import { cookies } from "next/headers";

export async function GET(req, { params }) {
    const cookie = await cookies();
    const token = cookie.get("token")?.value;

    if (!token) {
        return new Response(JSON.stringify({ error: 'Unauthorized' }), {
            status: 401,
            headers: {
                'Content-Type': 'application/json'
            }
        });
    }

    const { consultationId } = await params;

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

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

        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 });
    }
}