export type JobStatus =
  | "created"
  | "ingesting"
  | "ai_processing"
  | "pending_qc"
  | "approved_english"
  | "approved_source"  // For non-English source videos
  | "rejected"
  | "qc_feedback"
  | "translating"
  | "tts_generating"
  | "pending_final_review"
  | "completed";

export type UserRole = "client" | "reviewer" | "production" | "admin";
export type AuthProvider = "local" | "microsoft";

export interface User {
  id: string;
  email: string;
  full_name: string;
  role: UserRole;
  auth_provider: AuthProvider;
  is_active: boolean;
  created_at: string;
}

export interface Source {
  filename: string;
  original_filename?: string;
  client_name?: string;
  video_gcs?: string;
  gcs_uri: string;
  duration_s?: number;
  language: string;  // Final source language (from detection or explicit)
  language_hint?: string;  // User-provided hint for non-English videos
  detected_language?: string;  // AI-detected language from Gemini
}

export type TTSProvider = "gemini" | "google" | "elevenlabs";
export type TTSModel = "flash" | "pro";
export type TTSStylePreset = "neutral" | "calm" | "energetic" | "professional" | "warm" | "documentary" | "custom";

export interface TTSPreferences {
  provider: TTSProvider;
  default_voice: string;
  voices_per_language: Record<string, string>;
  model: TTSModel;
  speed: number;
  style_preset: TTSStylePreset;
  custom_style_prompt?: string;
}

export interface RequestedOutputs {
  captions_vtt: boolean;
  audio_description_vtt: boolean;
  audio_description_mp3: boolean;
  languages: string[];
  transcreation: string[];
  tts_preferences?: TTSPreferences;
}

export interface VoicesResponse {
  voices: string[];
  default: string;
}

export interface LanguagesResponse {
  languages: Record<string, string>;
  preview_samples: Record<string, string>;
}

export interface TTSOptionItem {
  value: string;
  label: string;
}

export interface SpeedRange {
  min: number;
  max: number;
  default: number;
  step: number;
}

export interface TTSOptionsResponse {
  models: TTSOptionItem[];
  style_presets: TTSOptionItem[];
  speed_range: SpeedRange;
}

export interface LangOutput {
  captions_vtt_gcs?: string;
  ad_vtt_gcs?: string;
  ad_mp3_gcs?: string;
  origin?: "translate" | "transcreate";
  qa_notes?: string;
}

export interface ReviewHistoryItem {
  at: string;
  status: string;
  by?: string;
  notes?: string;
}

export interface Review {
  notes?: string;
  reviewer_id?: string;
  history: ReviewHistoryItem[];
}

export interface AISection {
  ingestion_json?: Record<string, unknown>;
  confidence?: number;
}

export interface Job {
  id: string;
  client_id: string;
  title: string;
  source: Source;
  requested_outputs: RequestedOutputs;
  status: JobStatus;
  review: Review;
  outputs?: Record<string, LangOutput>;
  ai?: AISection;
  error?: Record<string, unknown>;
  created_at: string;
  updated_at: string;
}

export interface LoginRequest {
  email: string;
  password: string;
}

export interface LoginResponse {
  access_token: string;
  token_type: string;
  user_id: string;
  role: string;
}

export interface RefreshResponse {
  access_token: string;
  token_type: string;
  user_id: string;
  role: string;
  email: string;
  full_name: string;
}

export interface MicrosoftLoginRequest {
  id_token: string;
}

export interface MicrosoftLoginResponse {
  access_token: string;
  token_type: string;
  user_id: string;
  role: string;
  email: string;
  full_name: string;
  auth_provider: AuthProvider;
}

export interface JobCreateRequest {
  title: string;
  source_is_english: boolean;  // True = English source, False = other language (auto-detect)
  language_hint?: string;  // Optional hint when source_is_english=false
  requested_outputs: RequestedOutputs;
}

export interface JobListResponse {
  jobs: Job[];
  total: number;
  page: number;
  size: number;
}

export interface JobDownloadsResponse {
  downloads: Record<string, Record<string, string> | string>;
}

export interface VttContentResponse {
  captions_vtt?: string;
  audio_description_vtt?: string;
}

export interface VttUpdateRequest {
  captions_vtt?: string;
  audio_description_vtt?: string;
  language?: string;  // If not specified, defaults to source language
}

export interface AssetValidationResponse {
  is_valid: boolean;
  errors: string[];
  warnings: string[];
}

export interface BulkDeleteRequest {
  job_ids: string[];
}

export interface BulkDeleteResponse {
  deleted_count: number;
  total_requested: number;
  errors: string[];
}

export interface JobDeleteResponse {
  message: string;
}

// User Management types
export interface UserListResponse {
  users: User[];
  total: number;
  page: number;
  size: number;
}

export interface CreateUserRequest {
  email: string;
  password: string;
  full_name: string;
  role: UserRole;
}

export interface UpdateUserRequest {
  email?: string;
  full_name?: string;
  role?: UserRole;
  is_active?: boolean;
}

export interface ResetPasswordResponse {
  message: string;
  temporary_password: string;
  note: string;
}

export interface AdminStatsResponse {
  total_users: number;
  total_jobs: number;
  jobs_by_status: Record<string, number>;
  active_jobs_today: number;
  avg_processing_time_hours: number;
}