/**
 * OCR Providers Module
 * 导出所有 OCR 提供商
 */

export { BaseOCRProvider } from './base.provider';
export type { IImageSource, OCRRecognitionResult, OCRProviderConfig } from './base.provider';
export { TesseractProvider, tesseractProvider } from './tesseract.provider';
export { BaiduProvider, baiduProvider } from './baidu.provider';
export { RapidOCRProvider, rapidocrProvider } from './rapidocr.provider';
export { PaddleOCRProvider, paddleocrProvider } from './paddleocr.provider';

import { TesseractProvider } from './tesseract.provider';
import { BaiduProvider } from './baidu.provider';
import { RapidOCRProvider } from './rapidocr.provider';
import { PaddleOCRProvider } from './paddleocr.provider';

/**
 * OCR Provider 类型
 */
export type OCRProviderType = 'tesseract' | 'baidu' | 'rapidocr' | 'paddleocr' | 'auto';

/**
 * OCR Provider 工厂
 * 根据 provider 类型返回对应的实例
 */
export class OCRProviderFactory {
  private static providers = {
    tesseract: TesseractProvider,
    baidu: BaiduProvider,
    rapidocr: RapidOCRProvider,
    paddleocr: PaddleOCRProvider,
  };

  /**
   * 创建 Provider 实例
   */
  static create(
    type: OCRProviderType,
    config?: any
  ): TesseractProvider | BaiduProvider | RapidOCRProvider | PaddleOCRProvider {
    if (type === 'auto') {
      // 自动选择可用的 provider
      return this.autoSelect();
    }

    const ProviderClass = this.providers[type];
    if (!ProviderClass) {
      throw new Error(`未知的 OCR provider: ${type}`);
    }

    return new ProviderClass(config);
  }

  /**
   * 自动选择可用的 provider
   * 优先级: PaddleOCR > RapidOCR > Tesseract > Baidu
   */
  private static autoSelect(): TesseractProvider | BaiduProvider | RapidOCRProvider | PaddleOCRProvider {
    const envProvider = process.env.OCR_PROVIDER as OCRProviderType;

    // 如果指定了 provider 且不是 auto，使用指定的
    if (envProvider && envProvider !== 'auto' && this.providers[envProvider]) {
      const ProviderClass = this.providers[envProvider];
      return new ProviderClass();
    }

    // 检查可用性并选择
    // PaddleOCR (本地高精度)
    if (process.env.PADDLEOCR_API_URL) {
      return new PaddleOCRProvider();
    }

    // RapidOCR (本地快速)
    if (process.env.RAPIDOCR_API_URL) {
      return new RapidOCRProvider();
    }

    // Baidu OCR (云端准确)
    if (process.env.BAIDU_OCR_API_KEY && process.env.BAIDU_OCR_SECRET_KEY) {
      return new BaiduProvider();
    }

    // Tesseract.js (本地兜底)
    return new TesseractProvider();
  }

  /**
   * 获取所有可用的 providers
   */
  static async getAvailableProviders(): Promise<
    Array<{ type: string; name: string; available: boolean; typeDesc: string }>
  > {
    const providers = [
      { type: 'paddleocr', name: 'PaddleOCR', instance: new PaddleOCRProvider(), typeDesc: '本地高精度' },
      { type: 'rapidocr', name: 'RapidOCR', instance: new RapidOCRProvider(), typeDesc: '本地快速准确' },
      { type: 'baidu', name: 'Baidu OCR', instance: new BaiduProvider(), typeDesc: '云端准确' },
      { type: 'tesseract', name: 'Tesseract.js', instance: new TesseractProvider(), typeDesc: '本地轻量' },
    ];

    const results = await Promise.all(
      providers.map(async (p) => ({
        type: p.type,
        name: p.name,
        available: await p.instance.isAvailable(),
        typeDesc: p.typeDesc,
      }))
    );

    return results;
  }
}
