export interface ProfileStep {
  step: number;
  title: string;
  completed: boolean;
}

export interface ProfileProgress {
  completion_percentage: number;
  current_step: number;
  profile_status: string;
  is_profile_completed: boolean;
  steps: ProfileStep[];
  next_screen?: string;
  admin_status?: string;
  pending_step?: number;
}

/*
* Step number -> the page that collects it. Order and titles match the
* real `/astrologer/profile/progress` response (confirmed against the
* "Register step N of 10" headers in the astrologer-ui mockups).
*/
export const STEP_ROUTES: Record<number, string> = {
  1: "/astrologer/profile/basic",
  2: "/astrologer/profile/languages",
  3: "/astrologer/profile/skills",
  4: "/astrologer/profile/photo",
  5: "/astrologer/profile/education",
  6: "/astrologer/profile/professional",
  7: "/astrologer/profile/experience",
  8: "/astrologer/profile/bank",
  9: "/astrologer/profile/documents",
  10: "/astrologer/profile/availability",
};

// Same steps, but the dedicated edit-mode page for each — used once an
// astrologer already has a profile (dashboard shell, "Update Profile"
// button, no site Header/Footer) instead of the first-time
// registration wizard page at the STEP_ROUTES path.
export const EDIT_STEP_ROUTES: Record<number, string> = {
  1: "/astrologer/profile/edit/basic",
  2: "/astrologer/profile/edit/languages",
  3: "/astrologer/profile/edit/skills",
  4: "/astrologer/profile/edit/photo",
  5: "/astrologer/profile/edit/education",
  6: "/astrologer/profile/edit/professional",
  7: "/astrologer/profile/edit/experience",
  8: "/astrologer/profile/edit/bank",
  9: "/astrologer/profile/edit/documents",
  10: "/astrologer/profile/edit/availability",
};

export const TOTAL_STEPS = Object.keys(STEP_ROUTES).length;

/*
* The single source of truth for "where should this astrologer land" —
* used right after login and whenever /astrologer/profile/progress
* loads. Confirmed live against the real backend: both the
* verify-login response and GET /astrologer/profile/progress carry a
* `next_screen` field that tells us exactly which of the three states
* the astrologer is in:
*   "create_profile"        -> still filling in the 10-step wizard
*   "profile_under_review"  -> fully submitted, awaiting admin approval
*   anything else           -> approved/verified -> the profile hub
* Always trusts this fresh signal rather than any locally cached step,
* so logging out mid-wizard (or while pending review) and back in
* lands on the correct screen automatically.
*/
export function getNextStepRoute(progress: ProfileProgress): string {
  if (progress.next_screen === "profile_under_review") {
    return "/astrologer/profile/review";
  }

  if (!progress.is_profile_completed) {
    const nextIncomplete = progress.steps
      ?.slice()
      .sort((a, b) => a.step - b.step)
      .find((s) => !s.completed);

    const stepNumber =
      nextIncomplete?.step ?? progress.pending_step ?? progress.current_step ?? 1;

    return STEP_ROUTES[stepNumber] ?? STEP_ROUTES[1];
  }

  // Fully submitted and no longer pending review -> approved -> Home.
  return "/astrologer/dashboard";
}
