import { z } from 'zod';
import { registerTool } from '../tools/registry.js';
import {
  extractionRun,
  extractionModels,
  extractionCacheStats,
  extractionSidecarHealth,
} from '../../lib/extraction-client.js';

registerTool({
  name: 'extraction.run',
  description:
    'Run text extraction using the extraction-service. Returns typed ExtractionItem[] array. Requires admin role.',
  requiredRole: 'admin',
  inputSchema: z.object({
    text: z.string().min(1).describe('Text to extract from'),
    taskId: z
      .string()
      .optional()
      .describe('Extraction task ID (e.g. triage, memory-insight, reflection-enrichment)'),
    modelId: z.string().optional().describe('Override model ID'),
  }),
  async execute(args, req) {
    return extractionRun(args, { requestId: req.id });
  },
});

registerTool({
  name: 'extraction.models',
  description: 'List available extraction model providers. Requires admin role.',
  requiredRole: 'admin',
  inputSchema: z.object({}),
  async execute(_args, req) {
    return extractionModels({ requestId: req.id });
  },
});

registerTool({
  name: 'extraction.cacheStats',
  description: 'Get extraction result cache statistics. Requires admin role.',
  requiredRole: 'admin',
  inputSchema: z.object({}),
  async execute(_args, req) {
    return extractionCacheStats({ requestId: req.id });
  },
});

registerTool({
  name: 'extraction.sidecarHealth',
  description:
    'Check the health of the Python extraction sidecar process. Returns status and last-seen timestamp. Requires admin role.',
  requiredRole: 'admin',
  inputSchema: z.object({}),
  async execute(_args, req) {
    return extractionSidecarHealth({ requestId: req.id });
  },
});
