import type { LocalePair } from "../../certification/types/certification";
import type { EstimatedTime } from "../../question/types/question";
import type { QuestionForm } from "../../question/types/question";
export type { QuestionForm };

export type ExamType = "WRITTEN" | "ORAL";
export type ExamAdministrationMethod = "OFFLINE" | "ONLINE";
export type ExamEvaluationMethod = "SUM" | "AVERAGE" | "PERCENTAGE";
export type ExamSelectionMethod = "MANUAL" | "RANDOM";
export type ExamStatus =
  | "PENDING"
  | "ACTIVE"
  | "COMPLETED"
  | "CANCELLED"
  | "POSTPONED"
  | "PUBLISHED";

export interface QuestionBrief {
  id: string;
  title: LocalePair;
  form: QuestionForm;
  points: number;
}

export interface Exam {
  id: string;
  certificationId: string;
  certification?: { id: string; name: LocalePair };
  title: LocalePair;
  direction: LocalePair | null;
  date: string;
  givenTime: EstimatedTime | null;
  type: ExamType;
  form: QuestionForm;
  venue: string;
  administrationMethod: ExamAdministrationMethod;
  evaluationMethod: ExamEvaluationMethod;
  selectionMethod: ExamSelectionMethod;
  cuttingPoint: number;
  status: ExamStatus;
  questions?: QuestionBrief[];
  createdAt: string;
  updatedAt: string;
}

export interface ListResponse<T> {
  total: number;
  items: T[];
}

export interface CreateExamPayload {
  certificationId: string;
  title: LocalePair;
  direction?: LocalePair;
  date: string;
  givenTime: EstimatedTime;
  type: ExamType;
  form: QuestionForm;
  venue: string;
  administrationMethod: ExamAdministrationMethod;
  evaluationMethod: ExamEvaluationMethod;
  selectionMethod?: ExamSelectionMethod;
  cuttingPoint: number;
}

export interface UpdateExamPayload {
  id: string;
  certificationId?: string;
  title?: LocalePair;
  direction?: LocalePair;
  date?: string;
  givenTime?: EstimatedTime;
  type?: ExamType;
  form?: QuestionForm;
  venue?: string;
  administrationMethod?: ExamAdministrationMethod;
  evaluationMethod?: ExamEvaluationMethod;
  selectionMethod?: ExamSelectionMethod;
  cuttingPoint?: number;
  status?: ExamStatus;
}

export interface AssignQuestionsPayload {
  examId: string;
  questionIds: string[];
  remark?: LocalePair;
}

export interface RandomQuestionsPayload {
  examId: string;
  count: number;
}

/** What the invigilator recorded on the day (US-EXAM-009). */
export type AttendanceStatus =
  | 'REGISTERED'
  | 'PRESENT'
  | 'ABSENT'
  | 'LATE'
  | 'WITHDRAWN'
  | 'DISQUALIFIED';

export interface ExamRegistration {
  id: string;
  examId: string;
  profileId: string;
  admissionNumber: string;
  kind: 'NEW' | 'RETAKE';
  attemptNumber: number;
  attendanceStatus: AttendanceStatus;
  attendanceRemark: string | null;
  attendanceRecordedAt: string | null;
  createdAt: string;
  profile?: {
    id: string;
    firstName: string | null;
    middleName: string | null;
    lastName: string | null;
    seafarerNumber: string | null;
  };
}

export interface RecordAttendancePayload {
  registrationId: string;
  status: AttendanceStatus;
  remark?: string;
}

export type ExamIncidentType =
  | 'MISCONDUCT'
  | 'TECHNICAL_FAILURE'
  | 'MEDICAL'
  | 'ADMINISTRATIVE'
  | 'OTHER';

export type ExamIncidentStatus =
  | 'OPEN'
  | 'UNDER_REVIEW'
  | 'RESOLVED'
  | 'DISMISSED';

export interface ExamIncident {
  id: string;
  examId: string;
  registrationId: string | null;
  type: ExamIncidentType;
  occurredAt: string;
  description: string;
  status: ExamIncidentStatus;
  resolution: string | null;
  resolvedAt: string | null;
  registration?: ExamRegistration;
}

export interface CreateIncidentPayload {
  examId: string;
  type: ExamIncidentType;
  registrationId?: string;
  description: string;
  occurredAt?: string;
}

export interface ResolveIncidentPayload {
  incidentId: string;
  outcome: 'RESOLVED' | 'DISMISSED';
  resolution: string;
}
