/**
 * Dev-only: manually trigger the appointment reminder service.
 *
 * Gated on ENABLE_DEV_ROUTES=1. Bypasses CRON_SECRET and can optionally run
 * in dryRun mode (renders messages without sending to Twilio or marking the
 * booking).
 *
 * POST /api/dev/run-reminders
 *   body: { dryRun?: boolean, nowMs?: number }
 */

import { NextRequest, NextResponse } from "next/server";
import { appointmentReminderService } from "@/lib/services/appointment-reminder.service";

export async function POST(req: NextRequest) {
  if (process.env.ENABLE_DEV_ROUTES !== "1") {
    return NextResponse.json({ error: "Dev routes disabled" }, { status: 403 });
  }
  try {
    const body = await req.json().catch(() => ({}));
    const dryRun = body?.dryRun === true;
    const nowMs =
      typeof body?.nowMs === "number" && Number.isFinite(body.nowMs)
        ? body.nowMs
        : undefined;

    const result = await appointmentReminderService.processAppointmentReminders(
      { dryRun, nowMs },
    );

    return NextResponse.json({ ok: true, dryRun, ...result });
  } catch (error) {
    return NextResponse.json(
      {
        ok: false,
        error: String(error instanceof Error ? error.message : error),
      },
      { status: 500 },
    );
  }
}
