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 ExamResultStatus = 'PASSED' | 'FAILED';

/** How far a mark has travelled through quality control (US-EXAM-011 → 014). */
export type ResultReviewStatus = 'MARKED' | 'MODERATED' | 'APPROVED' | 'PUBLISHED';

export type ExamAppealStatus =
  | 'SUBMITTED'
  | 'UNDER_REVIEW'
  | 'UPHELD'
  | 'REJECTED';

export interface ResultBreakdown {
  questionId: string;
  score: number;
  remark?: string;
}

export interface Profile {
  id: string;
  firstName: string;
  middleName?: string;
  lastName: string;
  gender?: string;
  dob?: string;
  maritalStatus?: string;
  type?: string;
  isComplete?: boolean;
}

export interface FullExam {
  id: string;
  certificationId?: string;
  title: LocalePair;
  direction?: LocalePair | null;
  date?: string;
  givenTime?: EstimatedTime | null;
  type?: string;
  form?: QuestionForm;
  venue?: string;
  administrationMethod?: string;
  evaluationMethod?: string;
  selectionMethod?: string;
  cuttingPoint?: number;
  status?: string;
  questions?: { id: string; title: LocalePair; form: QuestionForm; points: number }[];
}

export interface Result {
  id: string;
  seafarerId: string;
  seafarer?: { id: string; firstName: string; middleName?: string; lastName: string };
  examId: string;
  exam?: FullExam;
  profile?: Profile;
  resultBreakdowns: ResultBreakdown[];
  totalScore: number;
  remark: { en: string; am: string } | null;
  status: ExamResultStatus;
  reviewStatus: ResultReviewStatus;
  markedById: string | null;
  markedAt: string | null;
  preModerationScore: number | null;
  moderationAdjustment: number | null;
  moderationRemark: string | null;
  approvalRemark: string | null;
  approvedAt: string | null;
  publishedAt: string | null;
  createdAt: string;
  updatedAt: string;
}

/** A candidate's challenge to a published result (US-EXAM-016). */
export interface ExamAppeal {
  id: string;
  appealNumber: string;
  resultId: string;
  profileId: string;
  reason: string;
  status: ExamAppealStatus;
  decisionRemark: string | null;
  decidedAt: string | null;
  createdAt: string;
  result?: Result;
  profile?: Profile;
}

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

/** The total and the outcome are both computed server-side (US-EXAM-011). */
export interface CreateResultPayload {
  seafarerId: string;
  examId: string;
  resultBreakdowns: ResultBreakdown[];
  remark?: { en: string; am: string };
}

/**
 * `status` is absent on purpose: PASSED/FAILED is derived on the server from
 * the session's cutting point and evaluation method (US-EXAM-011).
 */
export interface UpdateResultPayload {
  id: string;
  resultBreakdowns?: ResultBreakdown[];
  remark?: { en: string; am: string };
}

export interface ModerateResultPayload {
  id: string;
  adjustment: number;
  remark: string;
}

export interface ResultDecisionPayload {
  id: string;
  remark: string;
}

export interface DecideAppealPayload {
  id: string;
  outcome: 'UPHELD' | 'REJECTED';
  remark: string;
}
