/**
 * @ai-summary Fastify routes for OCR API
 */
import { FastifyInstance, FastifyPluginAsync, FastifyPluginOptions } from 'fastify';
import { requireTier } from '../../../core/middleware/require-tier';
import { OcrController } from './ocr.controller';

export const ocrRoutes: FastifyPluginAsync = async (
  fastify: FastifyInstance,
  _opts: FastifyPluginOptions
) => {
  const ctrl = new OcrController();
  const requireAuth = fastify.authenticate.bind(fastify);

  // POST /api/ocr/extract - Synchronous OCR extraction
  fastify.post('/ocr/extract', {
    preHandler: [requireAuth],
    handler: ctrl.extract.bind(ctrl),
  });

  // POST /api/ocr/extract/vin - VIN-specific OCR extraction
  fastify.post('/ocr/extract/vin', {
    preHandler: [requireAuth],
    handler: ctrl.extractVin.bind(ctrl),
  });

  // POST /api/ocr/extract/receipt - Receipt-specific OCR extraction (Pro tier required)
  fastify.post('/ocr/extract/receipt', {
    preHandler: [requireAuth, requireTier('fuelLog.receiptScan')],
    handler: ctrl.extractReceipt.bind(ctrl),
  });

  // POST /api/ocr/extract/maintenance-receipt - Maintenance receipt OCR extraction (Pro tier required)
  fastify.post('/ocr/extract/maintenance-receipt', {
    preHandler: [requireAuth, requireTier('maintenance.receiptScan')],
    handler: ctrl.extractMaintenanceReceipt.bind(ctrl),
  });

  // POST /api/ocr/extract/manual - Manual extraction (Pro tier required)
  fastify.post('/ocr/extract/manual', {
    preHandler: [requireAuth, fastify.requireTier({ featureKey: 'document.scanMaintenanceSchedule' })],
    handler: ctrl.extractManual.bind(ctrl),
  });

  // POST /api/ocr/jobs - Submit async OCR job
  fastify.post('/ocr/jobs', {
    preHandler: [requireAuth],
    handler: ctrl.submitJob.bind(ctrl),
  });

  // GET /api/ocr/jobs/:jobId - Get job status
  fastify.get<{ Params: { jobId: string } }>('/ocr/jobs/:jobId', {
    preHandler: [requireAuth],
    handler: ctrl.getJobStatus.bind(ctrl),
  });
};
