/**
 * Extraction service client factory.
 * Uses @bytelyst/api-client under the hood for consistent auth token injection.
 */

import { createApiClient } from '@bytelyst/api-client';

import type {
  ExtractionClientConfig,
  ExtractRequest,
  ExtractResponse,
  BatchExtractRequest,
  BatchExtractResponse,
  ExtractionTask,
  TranscribeRequest,
  TranscribeResponse,
} from './types.js';

export interface ExtractionClient {
  /** Single document extraction. */
  extract(req: ExtractRequest): Promise<ExtractResponse>;

  /** Batch extraction (multiple inputs, shared config). */
  extractBatch(req: BatchExtractRequest): Promise<BatchExtractResponse>;

  /** List available extraction tasks. */
  listTasks(productId?: string): Promise<ExtractionTask[]>;

  /** Get a single task by ID. */
  getTask(id: string, productId?: string): Promise<ExtractionTask>;

  /** Transcribe audio from a URL via the configured STT provider. */
  transcribe(req: TranscribeRequest): Promise<TranscribeResponse>;
}

/**
 * Create a typed extraction service client.
 *
 * @example
 * ```ts
 * const client = createExtractionClient({
 *   baseUrl: "http://localhost:4005",
 *   getToken: () => localStorage.getItem("access_token"),
 * });
 *
 * const result = await client.extract({
 *   text: "John said we should ship by Friday.",
 *   taskId: "transcript-extraction",
 * });
 * ```
 */
export function createExtractionClient(config: ExtractionClientConfig): ExtractionClient {
  const api = createApiClient({
    baseUrl: config.baseUrl,
    getToken: config.getToken,
  });

  return {
    async extract(req: ExtractRequest): Promise<ExtractResponse> {
      return api.fetch<ExtractResponse>('/api/extract', {
        method: 'POST',
        body: JSON.stringify(req),
      });
    },

    async extractBatch(req: BatchExtractRequest): Promise<BatchExtractResponse> {
      return api.fetch<BatchExtractResponse>('/api/extract/batch', {
        method: 'POST',
        body: JSON.stringify(req),
      });
    },

    async listTasks(productId?: string): Promise<ExtractionTask[]> {
      const qs = productId ? `?productId=${encodeURIComponent(productId)}` : '';
      return api.fetch<ExtractionTask[]>(`/api/tasks${qs}`);
    },

    async getTask(id: string, productId?: string): Promise<ExtractionTask> {
      const qs = productId ? `?productId=${encodeURIComponent(productId)}` : '';
      return api.fetch<ExtractionTask>(`/api/tasks/${encodeURIComponent(id)}${qs}`);
    },

    async transcribe(req: TranscribeRequest): Promise<TranscribeResponse> {
      return api.fetch<TranscribeResponse>('/api/transcribe', {
        method: 'POST',
        body: JSON.stringify(req),
      });
    },
  };
}
