import { NextRequest, NextResponse } from "next/server";
import { prisma } from "@/lib/prisma";
import { getAdminUser } from "@/lib/auth";

// GET /api/admin/timeslots/range?start_date=2026-07-01&end_date=2026-07-31
// Returns every generated slot in the range so the admin UI can display them, not hide them
export async function GET(req: NextRequest) {
  const admin = await getAdminUser();
  if (!admin) return NextResponse.json({ error: "Unauthorized" }, { status: 401 });

  const startDate = req.nextUrl.searchParams.get("start_date");
  const endDate = req.nextUrl.searchParams.get("end_date");

  if (!startDate || !endDate) {
    return NextResponse.json({ error: "start_date and end_date are required" }, { status: 400 });
  }

  const slots = await prisma.time_slots.findMany({
    where: { slot_date: { gte: new Date(startDate), lte: new Date(endDate) } },
    orderBy: [{ slot_date: "asc" }, { start_time: "asc" }],
  });

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