/**
 * Which step owns which required fields.
 *
 * Extracted from the form so the rule "Next checks only the step in front of
 * the user" is testable without rendering a modal — and so the final submit
 * and the per-step check can never drift apart, since both read this.
 */
export interface ExamFormValues {
  certificationId: string | null;
  titleEn: string;
  titleAm: string;
  directionEn: string;
  directionAm: string;
  date: string;
  venue: string;
  type: string | null;
  form: string | null;
  adminMethod: string | null;
  evalMethod: string | null;
  cuttingPoint: number;
}

/** Basic Info, Settings, Review — only the last one submits. */
export const LAST_STEP = 2;

export function validateBasic(v: ExamFormValues): string | null {
  if (!v.certificationId || !v.titleEn || !v.titleAm || !v.date || !v.venue) {
    return 'exam.form.fillRequiredBasic';
  }
  // Either both languages or neither: a direction in one language only reads
  // as missing to half the candidates.
  if ((v.directionEn || v.directionAm) && !(v.directionEn && v.directionAm)) {
    return 'exam.form.directionBothLanguages';
  }
  return null;
}

export function validateSettings(v: ExamFormValues): string | null {
  return !v.type || !v.form || !v.adminMethod || !v.evalMethod || !v.cuttingPoint
    ? 'exam.form.fillRequiredSettings'
    : null;
}

/** The step in front of the user, and nothing else. Review has no fields. */
export function validateStep(step: number, v: ExamFormValues): string | null {
  if (step === 0) return validateBasic(v);
  if (step === 1) return validateSettings(v);
  return null;
}

/**
 * The whole payload, as the final check before submitting.
 *
 * Not redundant with the per-step checks: the stepper header can be clicked
 * back to a visited step, so arriving at Review is no proof the user walked
 * here in order with everything still filled in.
 */
export function validateAll(v: ExamFormValues): string | null {
  return validateBasic(v) ?? validateSettings(v);
}

/** Where a failure has to land for the user to be able to fix it. */
export function stepOfError(error: string): number {
  return error === 'exam.form.fillRequiredSettings' ? 1 : 0;
}
