import { NextResponse } from 'next/server';
import { db } from "@/drizzle/db";
import { liveKillSwitches } from "@/drizzle/schema";

// PANOPTICON: Internal Kill List Endpoint
// Called by middleware (proxy.ts) to sync the in-memory kill cache.
// Protected by x-internal-secret header matching JWT_SECRET.
export async function GET(req: Request) {
  try {
    const internalSecret = req.headers.get('x-internal-secret');
    if (internalSecret !== process.env.JWT_SECRET) {
      return NextResponse.json({ error: 'Forbidden' }, { status: 403 });
    }

    // Fetch all active kill targets (not expired)
    const kills = await db.select({
      targetId: liveKillSwitches.targetId,
    }).from(liveKillSwitches);

    const targets = kills.map(k => k.targetId);

    return NextResponse.json({ targets });
  } catch (error: any) {
    console.error('[KILL-LIST ERROR]', error);
    return NextResponse.json({ targets: [] });
  }
}
