import { NextResponse } from "next/server";
import { prisma } from "@/lib/prisma";
import { getStudent } from "@/lib/studentAuth";

export async function GET() {
  const student = await getStudent();
  if (!student) return NextResponse.json({ error: "Unauthorized" }, { status: 401 });

  const schedules = await prisma.student_schedules.findMany({
    where: { student_id: student.id, status: "active" },
    include: {
      student_schedule_patterns: true,
      schedule_bookings: {
        where: { status: "active" },
        include: { time_slots: { select: { start_time: true, end_time: true } } },
        orderBy: { booking_date: "asc" },
      },
    },
    orderBy: { created_at: "desc" },
  });

  return NextResponse.json({ data: schedules });
}