import { NextRequest, NextResponse } from "next/server";
import { prisma } from "@/infrastructure/db/prisma";
import { withAuth, ROLES } from "@/middleware/auth";

export async function GET(request: NextRequest) {
  const result = await withAuth(request, [ROLES.ASSISTANT, ROLES.THERAPIST]);

  if (result instanceof NextResponse) {
    return result;
  }

  const { user } = result;

  const appointments = await prisma.appointment.findMany({
    include: {
      patient: {
        select: {
          name: true,
          phone: true,
        },
      },
    },
    orderBy: {
      date: "asc",
    },
    where: {
      date: {
        gte: new Date(),
      },
    },
  });

  return NextResponse.json({
    success: true,
    appointments,
  });
}
