import { NextResponse } from "next/server";
import { trackRecommendationOutcomes } from "@/lib/services/outcome-tracker";

export const dynamic = "force-dynamic";
export const maxDuration = 300;

const CRON_SECRET = process.env.CRON_SECRET ?? "";
function authOk(req: Request): boolean {
  if (!CRON_SECRET) return false;
  const h = req.headers.get("authorization") ?? "";
  return h === `Bearer ${CRON_SECRET}` || new URL(req.url).searchParams.get("secret") === CRON_SECRET;
}

export async function GET(req: Request) {
  if (!authOk(req)) return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
  const tracked = await trackRecommendationOutcomes();
  return NextResponse.json({ tracked });
}
