/**
 * OCDS API discovery — root endpoint.
 * GET /api/ocds → returns service description + links to releases / parties.
 */
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 base = `${origin}${url.pathname}`.replace(/\/$/, '');
  const body = {
    service: 'vreaudigital.ro OCDS API',
    version: '0.1.0',
    standard: {
      name: 'Open Contracting Data Standard',
      version: '1.1',
      schema: 'https://standard.open-contracting.org/schema/1__1__5/release-package-schema.json',
    },
    publisher: {
      name: 'vreaudigital.ro',
      uri: 'https://vreaudigital.ro',
      role: 'aggregator',
      sourceData: 'SEAP / e-licitatie.ro · TED EU · datagov.ro',
    },
    license: 'https://creativecommons.org/licenses/by/4.0/',
    publicationPolicy: `${origin}/api/ocds/policy`,
    endpoints: {
      releases: `${base}/releases`,
      releaseById: `${base}/release/{id}`,
      parties: `${base}/parties/{id}`,
      bulkByYear: `${base}/bulk/year/{YYYY}`,
      bulkManifest: `${base}/bulk`,
    },
    rateLimits: {
      anonymous: '60 req/min, 10K req/day',
      authenticated: 'higher tiers — see /api/pricing',
    },
    documentation: 'https://vreaudigital.ro/api',
    contact: 'api@vreaudigital.ro',
    notes: [
      'Coverage maps SEAP/TED announcements to OCDS releases. Bid-level data (bids[]), amendments and milestones are partial — see PUBLICATION_POLICY for details.',
      'OCID format: ocds-vreaudigital-{seap_announcement_id}',
      'Party IDs use scheme RO-CUI: RO-CUI-{cui_normalized_no_RO_prefix}',
    ],
  };

  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=86400',
    },
  });
};
