import { NextRequest, NextResponse } from "next/server";
import { getUpcomingReminders, setReminderStatus } from "@/lib/db";
import type { ReminderStatus } from "@/lib/db";

export async function GET() {
  return NextResponse.json(getUpcomingReminders());
}

export async function PUT(request: NextRequest) {
  try {
    const body = await request.json();
    const { table, id, status } = body;

    const validTables = ["mc_registrations", "group_bookings", "open_day_bookings"];
    const validStatuses = ["pending", "coming", "cancelled", null];

    if (!validTables.includes(table)) {
      return NextResponse.json({ error: "Invalid table" }, { status: 400 });
    }
    if (!id || typeof id !== "number") {
      return NextResponse.json({ error: "id is required" }, { status: 400 });
    }
    if (!validStatuses.includes(status)) {
      return NextResponse.json({ error: "Invalid status" }, { status: 400 });
    }

    setReminderStatus(
      table as "mc_registrations" | "group_bookings" | "open_day_bookings",
      id,
      status as ReminderStatus | null
    );
    return NextResponse.json({ ok: true });
  } catch (err) {
    console.error("[admin/reminders] error:", err);
    return NextResponse.json({ error: "Internal error" }, { status: 500 });
  }
}
