import { RoleType } from "@/lib/enums/RoleType";

/**
 * Where to send a user straight after their email is verified.
 *
 * Mirrors the routing login already performs, so the two entry points cannot
 * disagree about where an account belongs.
 *
 * The tier picked before signup deliberately plays no part here. Both tiers
 * are sold on /practitioner/subscription, and that page already preselects
 * the group billing cycle from the stored preference. /practice-manager is
 * for an account that already runs a practice: sending a practitioner there
 * before they have bought anything lands them on a page whose data calls they
 * are not yet entitled to.
 */
export type VerificationDestination = {
  role: string;
  isManaged: boolean;
  hasActiveSubscription: boolean;
  /** The active subscription's tier, when one exists. */
  subscriptionType: string | null;
};

const GROUP = "group";

export function postVerificationPath({
  role,
  isManaged,
  hasActiveSubscription,
  subscriptionType,
}: VerificationDestination): string {
  // The dashboard, not /individual/subscription: individuals pay per
  // screening, so that page is billing history only and shows a newly
  // verified account an empty table with nothing to act on. The dashboard
  // carries the Start Screening action, and is where login sends them too.
  if (role === RoleType.INDIVIDUALS) {
    return "/individual";
  }

  if (role !== RoleType.PRACTITIONERS) {
    // Admins do not sign up through this flow; anything else is unroutable.
    return "/login";
  }

  // Covered by a practice manager's group plan — there is nothing to buy.
  if (isManaged) {
    return "/practitioner";
  }

  if (hasActiveSubscription) {
    return subscriptionType === GROUP
      ? "/practice-manager/dashboard"
      : "/practitioner";
  }

  // Nothing bought yet — the page that sells both tiers.
  return "/practitioner/subscription";
}
