/**
 * TypeScript Type Definitions for TMS Permission System
 * Combined from backend-tms (automated) and tms-request-2.0 (manual)
 */

// ==================== TMS API Types ====================

export interface TmsCredentials {
  email: string;
  password: string;
  baseUrl: string;
}

export interface Subset {
  id: number;
  title: string;
  lang?: string;
  members: Record<string, string>; // email -> role (R/RW)
  episodes?: Record<string, any>;
}

export interface ProjectDetails {
  projectId: number;
  name?: string;
  subsets: Subset[];
  members: Record<string, string>; // Project level members: email -> role
}

export type PermissionType = 'R' | 'RW';

// ==================== Submission Types ====================

export interface SubmissionInput {
  submission_type: 'tms_permission';
  username_list: string[];
  ge_input: string; // Format: "1000 de\n1001 us final"
}

export interface ParsedGeInput {
  ge_id: string;
  lang: string;
  final: boolean; // If "final" keyword present
}

export interface SubmissionRecord {
  id?: string;
  submission_id: string;
  submission_type: 'tms_permission' | 'raw_download';
  status: 'pending' | 'processing' | 'completed' | 'failed';
  input?: SubmissionInput;
  results?: SubmissionResult[];
  error_message?: string;
  queue_position?: number;
  created_at: string;
  updated_at: string;
}

export interface SubmissionResult {
  ge_id: string;
  lang: string;
  tms_id: string;
  tms_url: string;
  username: string;       // Username TMS được xử lý
  status: 'success' | 'error';
  message: string;
  processed_at: string;
}

// ==================== API Response Types ====================

export interface ApiResponse<T = any> {
  success: boolean;
  data?: T;
  error?: string;
}

export interface SubmitResponse {
  submission_id: string;
  status: string;
  message: string;
}

export interface StatusResponse {
  submission_id: string;
  status: string;
  progress: {
    total: number;
    completed: number;
    failed: number;
  };
  results: SubmissionResult[];
  usernames: string[];
  ge_input: string;
  created_at: string;
  updated_at: string;
}

// ==================== User Search Types ====================

export interface UserSearchResult {
  email: string;
  name: string;
}

// ==================== MongoDB Types ====================

export interface TitleData {
  geId: string;
  lang: string;
  tmsId?: string;
  trTmsLink?: string;
  path?: string;
}

// ==================== Worker Types ====================

export interface WorkerConfig {
  pollInterval: number;
  maxRetries: number;
}

// ==================== Check Upload Types ====================

export interface CheckInput {
  submission_type: 'check_upload';
  check_input: string; // Format: "4164 FR 3"
}

export interface ParsedCheckInput {
  ge_id: string;
  lang: string;
  chapter: string;
}

export interface CheckResult {
  ge_id: string;
  lang: string;
  chapter: string;
  status: 'FOUND' | 'NOT_FOUND' | 'ERROR';
  message: string;
  tms_url?: string;
}

export interface CheckRecord {
  id: string;
  created_at: string;
  input: CheckInput;
  status: 'pending' | 'processing' | 'completed' | 'failed';
  results: CheckResult[];
  error?: string;
}
