export type SupportedFormat = 'xlsx' | 'docx' | 'pptx';

export interface FileUploadState {
  /** First file — drives the single-file flow. */
  file: File | null;
  /** All queued files (1..MAX_BATCH_FILES) — drives the batch flow. */
  files: File[];
  /** i18n key (from ERROR_MESSAGES) — render through t() */
  error: string | null;
  isDragOver: boolean;
}

export interface FileUploadActions {
  handleDrop: (e: React.DragEvent) => void;
  handleDragOver: (e: React.DragEvent) => void;
  handleDragLeave: (e: React.DragEvent) => void;
  handleFileSelect: (e: React.ChangeEvent<HTMLInputElement>) => void;
  addFiles: (incoming: Iterable<File>) => void;
  removeAt: (index: number) => void;
  removeFile: () => void;
}

/** One file inside a running batch queue. */
export interface BatchItem {
  name: string;
  size: number;
  status: 'pending' | 'processing' | 'completed' | 'failed' | 'aborted';
  jobId?: string;
  progress: number;
  error?: string;
}

export interface UseFileUploadReturn extends FileUploadState, FileUploadActions {}

export type TranslationMode = 'classic' | 'llm';

/** Provider identifier — always matches the admin-side key. */
export type Provider = string;

export interface Language {
  code: string;
  name: string;
}

/** A provider returned by GET /api/v1/providers/available */
export interface AvailableProvider {
  id: Provider;
  label: string;
  description: string;
  mode: 'classic' | 'llm';
  /** LLM model used (e.g. deepseek/deepseek-v3.2) — same as admin config */
  model?: string;
}

export interface TranslationConfig {
  sourceLang: string;
  targetLang: string;
  mode: TranslationMode;
  provider?: Provider;
  pdfMode?: 'layout' | 'text_only';
  glossaryId?: string | null;
  translateImages?: boolean;
}

export interface UseTranslationConfigReturn {
  sourceLang: string;
  targetLang: string;
  /** Derived from selected provider — read-only. */
  mode: TranslationMode;
  provider: Provider | null;
  availableProviders: AvailableProvider[];
  isLoadingProviders: boolean;
  languages: Language[];
  isPro: boolean;
  isConfigValid: boolean;
  isLoadingLanguages: boolean;
  languagesError: string | null;
  setSourceLang: (lang: string) => void;
  setTargetLang: (lang: string) => void;
  setProvider: (provider: Provider | null) => void;
  glossaryId: string | null;
  setGlossaryId: (id: string | null) => void;
  translateImages: boolean;
  setTranslateImages: (val: boolean) => void;
  getConfig: () => TranslationConfig;
}

export type TranslationStatus = 'idle' | 'processing' | 'completed' | 'failed';

export interface TranslationJob {
  id: string;
  status: TranslationStatus;
  progress_percent: number;
  current_step: string;
  file_name?: string;
  source_lang?: string;
  target_lang?: string;
  created_at?: string;
  completed_at?: string;
  error_message?: string;
}

export interface TranslationSubmitResponse {
  data: TranslationJob;
  meta: {
    rate_limit_remaining?: number;
  };
}

export interface TranslationStatusResponse {
  data: TranslationJob;
  meta: {
    estimated_remaining_seconds?: number | null;
  };
}

export interface UseTranslationSubmitReturn {
  submitTranslation: (file: File, config: TranslationConfig) => Promise<void>;
  /** Ask the backend to cancel the running job. */
  cancelJob: () => Promise<boolean>;
  jobId: string | null;
  status: TranslationStatus;
  progress: number;
  currentStep: string;
  error: string | null;
  estimatedRemaining: number | null;
  fileName: string | null;
  reset: () => void;
  isSubmitting: boolean;
  isPolling: boolean;
  pollingFailures: number;
}
