export const prerender = false;

import type { APIRoute } from 'astro';
import { isAuthenticated, ADMIN_COOKIE } from '@admin/lib/admin-auth';

interface Env { DB: D1Database }

function getCtx(locals: App.Locals) {
  const runtime = (locals as any).runtime as { env: Env } | undefined;
  return runtime?.env;
}

export const GET: APIRoute = async ({ cookies, locals }) => {
  const env = getCtx(locals);
  if (!env?.DB || !await isAuthenticated(cookies.get(ADMIN_COOKIE)?.value, env.DB)) {
    return new Response(JSON.stringify({ error: 'Unauthorized' }), { status: 401 });
  }
  return new Response(
    JSON.stringify({ error: 'Deprecated: use /api/admin/trigger?kind=pipeline&target=<adapter-id>' }),
    { status: 410, headers: { 'Content-Type': 'application/json' } }
  );
};

export const PUT: APIRoute = async ({ request, cookies, locals }) => {
  const env = getCtx(locals);
  if (!env?.DB || !await isAuthenticated(cookies.get(ADMIN_COOKIE)?.value, env.DB)) {
    return new Response(JSON.stringify({ error: 'Unauthorized' }), { status: 401 });
  }
  return new Response(
    JSON.stringify({ error: 'Deprecated: pipeline jobs are queue-driven and not editable targets' }),
    { status: 410, headers: { 'Content-Type': 'application/json' } }
  );
};

export const POST: APIRoute = async ({ request, cookies, locals }) => {
  const env = getCtx(locals);
  if (!env?.DB || !await isAuthenticated(cookies.get(ADMIN_COOKIE)?.value, env.DB)) {
    return new Response(JSON.stringify({ error: 'Unauthorized' }), { status: 401 });
  }
  return new Response(
    JSON.stringify({ error: 'Deprecated: pipeline jobs are queue-driven and not creatable targets' }),
    { status: 410, headers: { 'Content-Type': 'application/json' } }
  );
};
