import { NextRequest, NextResponse } from "next/server";
import { tripService } from "@/server/services/trip.service";
import { payoutService } from "@/server/services/payout.service";
import { runCron } from "@/lib/cron-runner";

export const runtime = "nodejs";
export const dynamic = "force-dynamic";

/**
 * Cron daily — flip trip yang sudah lewat tanggal selesai dari OPEN/FULL ke
 * COMPLETED. Idempotent: run berulang aman.
 *
 * Trigger via system crontab (lihat [docs/CRON_SETUP.md](../../../docs/CRON_SETUP.md))
 * atau cron service apapun. Header wajib: `Authorization: Bearer ${CRON_SECRET}`.
 *
 * Set env `CRON_SECRET` (random ≥32 char) di hosting. Kalau env tidak di-set,
 * endpoint hard-fail 500 supaya tidak accidentally jalan tanpa proteksi.
 */
export async function GET(req: NextRequest) {
  const secret = process.env.CRON_SECRET;
  if (!secret) {
    console.error("[cron/auto-complete-trips] CRON_SECRET tidak di-set");
    return NextResponse.json(
      { error: "Server misconfigured" },
      { status: 500 }
    );
  }

  const authHeader = req.headers.get("authorization");
  if (authHeader !== `Bearer ${secret}`) {
    return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
  }

  const outcome = await runCron("auto-complete-trips", async () => {
    const result = await tripService.autoCompletePastTrips();
    // Setelah trip COMPLETED, payout yang sudah lewat heldUntil di-release
    // supaya admin bisa langsung transfer ke organizer. Idempotent.
    const releaseResult = await payoutService.releaseEligible();
    return {
      completed: result.count,
      ids: result.ids,
      payoutsReleased: releaseResult.releasedIds,
    };
  });

  if (!outcome.ok) {
    console.error("[cron/auto-complete-trips] gagal", outcome.error);
    return NextResponse.json(
      { error: "Gagal menjalankan auto-complete" },
      { status: 500 }
    );
  }
  console.log("[cron/auto-complete-trips] selesai", outcome.payload);
  return NextResponse.json({ ok: true, ...outcome.payload });
}
