import { config } from './config.js';

export interface ExtractionItem {
  extraction_class: string;
  extraction_text: string;
  attributes?: Record<string, string>;
}

export interface ExtractionResult {
  extractions: ExtractionItem[];
  metadata: {
    modelId: string;
    taskId?: string;
    durationMs?: number;
    cached?: boolean;
  };
}

export async function extractionRun(
  params: {
    text: string;
    taskId?: string;
    modelId?: string;
  },
  opts: { requestId?: string }
): Promise<ExtractionResult> {
  const url = `${config.EXTRACTION_SERVICE_URL}/api/extract`;
  const headers: Record<string, string> = {
    'Content-Type': 'application/json',
    ...(opts.requestId ? { 'x-request-id': opts.requestId } : {}),
  };
  const res = await fetch(url, {
    method: 'POST',
    headers,
    body: JSON.stringify(params),
    signal: AbortSignal.timeout(30_000),
  });
  if (!res.ok) {
    const body = await res.text().catch(() => '');
    throw new Error(`extraction-service POST /api/extract → ${res.status}: ${body}`);
  }
  return res.json() as Promise<ExtractionResult>;
}

export async function extractionModels(opts: { requestId?: string }): Promise<unknown> {
  const url = `${config.EXTRACTION_SERVICE_URL}/api/extract/models`;
  const headers: Record<string, string> = {
    ...(opts.requestId ? { 'x-request-id': opts.requestId } : {}),
  };
  const res = await fetch(url, { headers, signal: AbortSignal.timeout(5_000) });
  if (!res.ok) throw new Error(`extraction-service GET /api/extract/models → ${res.status}`);
  return res.json();
}

export async function extractionCacheStats(opts: { requestId?: string }): Promise<unknown> {
  const url = `${config.EXTRACTION_SERVICE_URL}/api/extract/cache-stats`;
  const headers: Record<string, string> = {
    ...(opts.requestId ? { 'x-request-id': opts.requestId } : {}),
  };
  const res = await fetch(url, { headers, signal: AbortSignal.timeout(5_000) });
  if (!res.ok) throw new Error(`extraction-service GET /api/extract/cache-stats → ${res.status}`);
  return res.json();
}

export async function extractionSidecarHealth(opts: { requestId?: string }): Promise<unknown> {
  const url = `${config.EXTRACTION_SERVICE_URL}/api/extract/sidecar-health`;
  const headers: Record<string, string> = {
    ...(opts.requestId ? { 'x-request-id': opts.requestId } : {}),
  };
  const res = await fetch(url, { headers, signal: AbortSignal.timeout(10_000) });
  return res.json();
}
