import type { StatusKind } from '@/components';
import { ROUTES } from '@/constants';
import type { VerificationStatus, VerificationStep, VerificationStepStatus } from '@/services/verification/types';

/**
 * Screen helpers shared across the verification subtree (B3/B4/B5/B6). Keeps the rendering **data-driven**:
 * the screens iterate `steps[]` and map each `code`/`status` to a label + chip via these helpers — a new
 * step type in the response renders without a code change. Labels are i18n keys off the `code`/`status`,
 * never derived from the enum string (honesty + localisation rule).
 */

/**
 * A display-only "mobile verified" step prepended to the checklist. It is **not** a server step — it is
 * satisfied at login (f1 phone-OTP), so it always renders `passed` and never blocks. Id 0 keeps it out
 * of the server-id space.
 */
export const MOBILE_STEP: VerificationStep = {
  id: 0,
  code: 'mobile_verified',
  displayName: 'mobile_verified',
  status: 'passed',
  isAutomated: true,
  expiresAt: null,
  failureReason: null,
};

/** The full ordered checklist as shown: the synthetic mobile step, then the server's seeded steps. */
export function displaySteps(status: VerificationStatus | undefined): VerificationStep[] {
  return status ? [MOBILE_STEP, ...status.steps] : [MOBILE_STEP];
}

/** "X از Y" meter counts — X = passed steps, Y = total (all seeded steps are required). */
export function progressCounts(status: VerificationStatus | undefined): { passed: number; total: number } {
  const steps = displaySteps(status);
  return { passed: steps.filter((step) => step.status === 'passed').length, total: steps.length };
}

/** The i18n label key for a step, keyed off its stable `code`. */
export function stepLabelKey(code: string): string {
  return `step_${code}`;
}

/** The i18n one-line description key for a step (what it verifies / why it is manual vs automatic). */
export function stepDescriptionKey(code: string): string {
  return `step_${code}_desc`;
}

interface StepChip {
  kind: StatusKind;
  labelKey: string;
}

/**
 * The chip kind + label key for a per-step status. Encodes the wireframe legend: green = passed,
 * amber = pending/in-review/expired, grey = not-started/next, red = failed. Expired is amber but keeps
 * its own label so copy stays honest ("expired", not "pending").
 */
export function stepStatusChip(status: VerificationStepStatus): StepChip {
  switch (status) {
    case 'passed':
      return { kind: 'verified', labelKey: 'status_passed' };
    case 'in_review':
      return { kind: 'pending', labelKey: 'status_in_review' };
    case 'pending':
      return { kind: 'pending', labelKey: 'status_pending' };
    case 'failed':
      return { kind: 'rejected', labelKey: 'status_failed' };
    case 'expired':
      return { kind: 'pending', labelKey: 'status_expired' };
    case 'not_started':
    default:
      return { kind: 'neutral', labelKey: 'status_next' };
  }
}

/** Which submission screen owns a given step code (drives the row CTA + the "continue" router). */
export function routeForStep(code: string): string | null {
  switch (code) {
    case 'identity_kyc':
    case 'shahkar_match':
      return ROUTES.NURSE_VERIFICATION_IDENTITY;
    case 'moh_competency_license':
    case 'ino_membership':
    case 'criminal_record':
      return ROUTES.NURSE_VERIFICATION_CREDENTIALS;
    case 'bank_account_verification':
      return ROUTES.NURSE_BANK;
    default:
      return null;
  }
}

/** A step the nurse can act on now — not passed, not waiting on an admin (`in_review`). */
function isActionable(step: VerificationStep): boolean {
  return step.status !== 'passed' && step.status !== 'in_review' && routeForStep(step.code) !== null;
}

/**
 * The route the B3 "continue" CTA targets: the first actionable step's screen (in checklist order); if
 * everything left is waiting on admin (`in_review`), the under-review screen; `null` when approved (the
 * publish CTA takes over).
 */
export function nextActionRoute(status: VerificationStatus | undefined): string | null {
  if (!status || status.status === 'approved') return null;
  const next = status.steps.find(isActionable);
  if (next) return routeForStep(next.code);
  if (status.steps.some((step) => step.status === 'in_review')) return ROUTES.NURSE_VERIFICATION_REVIEW;
  return null;
}

/** The 1-based index of the next actionable step (for the "continue to step N" CTA label). */
export function nextActionableIndex(status: VerificationStatus | undefined): number | null {
  if (!status) return null;
  const steps = displaySteps(status);
  const index = steps.findIndex(isActionable);
  return index === -1 ? null : index + 1;
}
