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

/**
 * The paper's own form — unlike a question's (QuestionForm), an exam may mix
 * both: BOTH means a mixed paper, resolved per-question against each
 * question's own ESSAY/CHOICE form. Mirrors backend EExamForm.
 */
export type ExamForm = QuestionForm | "BOTH";

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";

/** Only populated when the exam is fetched with `?i=questions,questions.options`. */
export interface QuestionOptionBrief {
  id: string;
  text: LocalePair;
  order: number;
}

export interface QuestionBrief {
  id: string;
  title: LocalePair;
  form: QuestionForm;
  points: number;
  options?: QuestionOptionBrief[];
}

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: ExamForm;
  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: ExamForm;
  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?: ExamForm;
  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;
  };
  /** The candidate's online sitting, when one has been started. */
  attempt?: {
    id: string;
    status: "IN_PROGRESS" | "SUBMITTED" | "EXPIRED";
  } | null;
}

export type RegradeOutcome =
  { graded: true; resultId: string } | { graded: false; reason: string };

/** One question's row on the staff grading sheet — the candidate's own
 * answer plus the auto-computable score, where one exists. */
export interface GradingSheetQuestion {
  questionId: string;
  form: QuestionForm;
  points: number;
  answerText: string | null;
  selectedOptionId: string | null;
  selectedOptionText: { en?: string; am?: string } | null;
  /** null means "no auto-score" — examiner enters one by hand. */
  autoScore: number | null;
}

export interface GradingSheet {
  attemptStatus: 'IN_PROGRESS' | 'SUBMITTED' | 'EXPIRED';
  questions: GradingSheetQuestion[];
}

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;
}
