export interface QuestionCategory {
  id: string;
  name: string;
  description: string | null;
  questions_count?: number;
  created_at: string;
  updated_at: string;
}

export interface CreateQuestionCategoryPayload {
  id?: string;
  name: string;
  description?: string | null;
}

export interface UpdateQuestionCategoryPayload extends CreateQuestionCategoryPayload {
  id: string;
}

export interface DeleteQuestionCategoryPayload {
  message: string;
  id?: string;
}

export interface QuestionLevel {
  id: string;
  name: string;
  color: string;
  created_at: string;
  updated_at: string;
}

export interface CreateQuestionLevelPayload {
  id?: string;
  name: string;
  color?: string;
}

export interface UpdateQuestionLevelPayload extends CreateQuestionLevelPayload {
  id: string;
}

export interface DeleteQuestionLevelPayload {
  message: string;
  id?: string;
}

export interface QuestionOption {
  id: string;
  option_text: string;
  is_correct: boolean;
  display_order: number;
}

export interface QuestionMatch {
  id: string;
  left_text: string;
  right_text: string;
  display_order: number;
}

export interface Question {
  id: string;
  category: QuestionCategory | null;
  level: QuestionLevel | null;
  type: 'mcq_single' | 'mcq_multiple' | 'match_column' | 'short_answer';
  question_text: string;
  explanation: string | null;
  points: number;
  options: QuestionOption[];
  matches: QuestionMatch[];
  created_by: string | null;
  created_at: string;
  updated_at: string;
}

export interface CreateQuestionPayload {
  category_id: string;
  level_id: string;
  type: string;
  question_text: string;
  explanation?: string | null;
  points: number;
  options?: { option_text: string; is_correct: boolean }[];
  matches?: { left_text: string; right_text: string }[];
}

export interface UpdateQuestionPayload extends CreateQuestionPayload {
  id: string;
}

export interface DeleteQuestionPayload {
  message: string;
  id?: string;
}

export interface ImportQuestionsPayload {
  file: File;
  category_id: string;
  level_id: string;
}

export interface ImportQuestionsResult {
  imported: number;
  failed: number;
  errors: { row: number; message: string }[];
}

export interface PaginationMeta {
  current_page: number;
  last_page: number;
  per_page: number;
  total: number;
}

export type QuizStatus = 'draft' | 'active' | 'archived';
export type QuizSelectionMode = 'manual' | 'automatic';
export type QuizType = 'internal' | 'recruitment';

export interface QuizAutoConfigRule {
  category_id: string;
  level_id: string | null;
  count: number;
}

export interface QuizQuestionEntry {
  quiz_question_id: string;
  display_order: number;
  question: Question;
}

export interface Quiz {
  id: string;
  name: string;
  description: string | null;
  type: QuizType;
  duration_minutes: number | null;
  max_attempts: number;
  pass_percentage: number;
  correct_score: string;
  incorrect_score: string;
  show_answers_after_submit: boolean;
  question_selection_mode: QuizSelectionMode;
  status: QuizStatus;
  questions_count: number;
  total_points: number;
  created_at: string;
  updated_at?: string;
}

export interface QuizDetail extends Quiz {
  questions: QuizQuestionEntry[];
  auto_config: { id: string; quiz_id: string; rules: QuizAutoConfigRule[] } | null;
}

export interface CreateQuizPayload {
  name: string;
  description?: string | null;
  type: QuizType;
  duration_minutes?: number | null;
  max_attempts: number;
  pass_percentage: number;
  correct_score: number;
  incorrect_score: number;
  show_answers_after_submit?: boolean;
  question_selection_mode: QuizSelectionMode;
  question_ids?: string[];
  rules?: QuizAutoConfigRule[];
}

export interface UpdateQuizPayload extends CreateQuizPayload {
  id: string;
}

export interface DeleteQuizPayload {
  message: string;
  id?: string;
}

// Sprint 3: Assignments, Attempts, Results

export type AttemptStatus = 'not_started' | 'in_progress' | 'submitted' | 'passed' | 'failed';
export type AssignmentTargetType = 'all_employees' | 'org_unit' | 'individual';

export interface RecurrencePattern {
  frequency: 'daily' | 'weekly' | 'monthly';
  interval: number;
  day_of_week?: number;
}

export interface QuizAssignment {
  id: string;
  quiz: Quiz | null;
  target_type: AssignmentTargetType;
  target_id: string | null;
  assigned_employee_ids: string[];
  start_at: string;
  end_at: string;
  is_periodic: boolean;
  recurrence_pattern: RecurrencePattern | null;
  notify_employees: boolean;
  created_at: string;
}

export interface QuizAssignmentDetail extends QuizAssignment {
  total_assigned: number;
  taken: number;
  not_taken: number;
  pass_count: number;
  fail_count: number;
}

export interface CreateAssignmentPayload {
  quiz_id: string;
  target_type: AssignmentTargetType;
  target_id?: string;
  start_at: string;
  end_at: string;
  notify_employees?: boolean;
  is_periodic?: boolean;
  recurrence_pattern?: RecurrencePattern;
}

export interface QuizAttempt {
  id: string;
  quiz_assignment_id: string;
  employee_id: string | null;
  employee_name?: string;
  started_at: string | null;
  submitted_at: string | null;
  time_remaining_seconds: number | null;
  attempt_number: number;
  score: string | null;
  max_score: string | null;
  percentage: string | null;
  status: AttemptStatus;
  question_ids: string[];
  created_at: string;
}

export interface AttemptAnswer {
  id: string;
  question_id: string;
  question: Question;
  selected_option_ids: string[] | null;
  match_answers: { left_text: string; right_text: string }[] | null;
  short_answer_text: string | null;
  is_correct: boolean | null;
  points_earned: string;
}

export interface QuizAttemptDetail extends QuizAttempt {
  answers: AttemptAnswer[];
  assignment?: QuizAssignment;
}

export interface MyAssignment {
  assignment: { id: string; start_at: string; end_at: string };
  quiz: {
    id: string;
    name: string;
    description: string | null;
    duration_minutes: number | null;
    max_attempts: number;
    pass_percentage: number;
  };
  my_attempts: QuizAttempt[];
  can_start: boolean;
  attempts_remaining: number;
}

export interface StartAttemptResponse {
  attempt: QuizAttempt;
  questions: {
    id: string;
    type: string;
    question_text: string;
    options: { id: string; option_text: string }[];
    matches: { left_text: string; right_text: string }[];
  }[];
  quiz: {
    name: string;
    duration_minutes: number | null;
    show_answers_after_submit: boolean;
  };
}

export interface AssessmentStats {
  total_assigned: number;
  total_taken: number;
  not_taken_count: number;
  pass_count: number;
  fail_count: number;
  average_percentage: number;
  highest_percentage: number;
  lowest_percentage: number;
  leaderboard: { employee_id: string; name: string; score: string; percentage: string }[];
}

export interface NonCompleter {
  employee_id: string;
  firstname: string;
  lastname: string;
  org_unit_name: string | null;
}

export interface QuestionAnalyticsItem {
  question_id: string;
  question_text: string;
  type: string;
  difficulty_index: number;
  discrimination_index: number;
  total_responses: number;
  correct_count: number;
  distractor_effectiveness: {
    option_id: string;
    option_text: string;
    selection_count: number;
    is_correct: boolean;
  }[];
}

export interface Assignee {
  employee_id: string;
  firstname: string;
  lastname: string;
  org_unit_name: string | null;
  attempts_count: number;
  latest_status: string;
  latest_score: string | null;
  latest_percentage: string | null;
}

// Sprint 4: Recruitment Assessments

export type CollectField = 'name' | 'email' | 'phone' | 'position_applied';

export interface RecruitmentAssessment {
  id: string;
  title: string;
  instructions: string | null;
  collect_fields: CollectField[];
  is_active: boolean;
  share_token: string;
  public_url: string;
  expires_at: string | null;
  quiz?: Quiz;
  candidates_count?: number;
  created_at: string;
}

export interface CreateRecruitmentAssessmentPayload {
  quiz_id: string;
  title: string;
  instructions?: string | null;
  collect_fields: CollectField[];
  expires_at?: string | null;
}

export interface UpdateRecruitmentAssessmentPayload {
  id: string;
  title?: string;
  instructions?: string | null;
  collect_fields?: CollectField[];
  is_active?: boolean;
  expires_at?: string | null;
}

export interface RecruitmentCandidate {
  id: string;
  name: string;
  email: string;
  phone: string | null;
  position_applied: string | null;
  custom_data: Record<string, string> | null;
  submitted_at: string | null;
  attempt?: {
    id: string;
    score: string | null;
    max_score: string | null;
    percentage: string | null;
    status: AttemptStatus;
    started_at: string | null;
    submitted_at: string | null;
  } | null;
  created_at: string;
}

export interface PublicAssessmentInfo {
  title: string;
  instructions: string | null;
  collect_fields: CollectField[];
  quiz: {
    name: string;
    duration_minutes: number | null;
    questions_count: number;
  };
}

export interface StartRecruitmentAttemptPayload {
  name: string;
  email: string;
  phone?: string;
  position_applied?: string;
}

export interface PublicStartResponse {
  attempt: {
    id: string;
    time_remaining_seconds: number | null;
    status: string;
    question_ids: string[];
  };
  questions: {
    id: string;
    type: string;
    question_text: string;
    options: { id: string; option_text: string }[];
    matches: { left_text: string; right_text: string }[];
  }[];
  quiz: {
    name: string;
    duration_minutes: number | null;
    show_answers_after_submit: boolean;
  };
  resumed: boolean;
  saved_answers: {
    question_id: string;
    selected_option_ids: string[] | null;
    match_answers: { left_text: string; right_text: string }[] | null;
    short_answer_text: string | null;
  }[];
}

export interface PublicSubmitResponse {
  score: string;
  max_score: string;
  percentage: string;
  status: string;
}
