/**
 * OCDS bulk discovery — links to per-year exports.
 * GET /api/ocds/bulk → manifest
 */
import type { APIRoute } from 'astro';
import { publicOrigin } from '../../../../lib/public-url.js';

export const prerender = false;

export const GET: APIRoute = async ({ url, request }) => {
  const origin = publicOrigin(request, url);
  const years: number[] = [];
  for (let y = 2007; y <= new Date().getUTCFullYear(); y++) years.push(y);

  const body = {
    service: 'vreaudigital.ro OCDS Bulk Export',
    description: 'Streaming JSONL exports of all OCDS releases by year. Each line is a Release object (https://standard.open-contracting.org/1.1/en/schema/release/), preceded by a header line starting with `# {package metadata}`.',
    license: 'https://creativecommons.org/licenses/by/4.0/',
    format: 'application/x-ndjson',
    note: 'Recommended: pipe through gzip (curl will accept-encoding gzip from Traefik). Use the paginated /api/ocds/releases endpoint for incremental sync.',
    years: years.reverse().map((y) => ({
      year: y,
      url: `${origin}/api/ocds/bulk/year/${y}`,
    })),
    examples: {
      curl: `curl -L --compressed -o ocds-2024.jsonl ${origin}/api/ocds/bulk/year/2024`,
      python: `import requests, json\\nwith requests.get('${origin}/api/ocds/bulk/year/2024', stream=True) as r:\\n    for line in r.iter_lines():\\n        if line.startswith(b'#'): continue\\n        release = json.loads(line)`,
    },
  };

  return new Response(JSON.stringify(body, null, 2), {
    status: 200,
    headers: {
      'Content-Type': 'application/json; charset=utf-8',
      'Access-Control-Allow-Origin': '*',
      'Cache-Control': 'public, max-age=3600',
    },
  });
};
