export const dynamic = 'force-dynamic';
import { NextResponse } from "next/server";
import { prisma } from "@/lib/prisma";
import { logAuditEvent } from "@/lib/audit";
import { TaskStatus } from "@agent-bounty/contracts";
import { releasePayment } from "@/lib/payment";
import { cronUnauthorizedResponse, isCronRequestAuthorized } from "@/lib/cron-auth";

// Optional: restrict to cron secret
// export const maxDuration = 60; // Next.js edge/serverless config

export async function GET(req: Request) {
  if (!isCronRequestAuthorized(req)) {
    return cronUnauthorizedResponse("Reaper");
  }

  // Find all claims that have expired, but the task is still EXECUTING
  const now = new Date();
  
  const expiredClaims = await prisma.claim.findMany({
    where: {
      status: TaskStatus.EXECUTING,
      expires_at: { lt: now },
    },
    include: {
      task: true,
    }
  });

  const rolledBackIds: string[] = [];

  for (const claim of expiredClaims) {
    if (claim.task.status === TaskStatus.EXECUTING) {
      await prisma.$transaction(async (tx) => {
        // Rollback Task
        await tx.task.update({
          where: { id: claim.task_id },
          data: {
            status: TaskStatus.OPEN,
            error_classification: "claim_timeout",
          }
        });

        // Rollback Claim
        await tx.claim.update({
          where: { id: claim.id },
          data: { status: "CANCELLED" }
        });

        await logAuditEvent(tx, {
          actorType: "SYSTEM",
          action: "CLAIM_TIMEOUT_REAPER",
          entityType: "TASK",
          entityId: claim.task_id,
          beforeState: { status: TaskStatus.EXECUTING },
          afterState: { status: TaskStatus.OPEN },
          reason: `Claim ${claim.id} expired at ${claim.expires_at.toISOString()}`,
        });

        await releasePayment(tx, claim.task_id, `${claim.id}-release`);
      });
      rolledBackIds.push(claim.task_id);
    }
  }

  return NextResponse.json({
    swept: rolledBackIds.length,
    task_ids: rolledBackIds,
  });
}
