export type ThankYouFlowKey =
  | "free-guide"
  | "guide-delivery"
  | "route-pack"
  | "order-pack"
  | "stay-enquiry"
  | "accommodation-help"
  | "business-onboarding"
  | "listing-onboarding"
  | "contact"
  | "trip-finder"
  | "lead";

export type ThankYouFlow = {
  key: ThankYouFlowKey;
  eyebrow: string;
  title: string;
  description: string;
  statusLabel: string;
  nextSteps: string[];
  primaryCta: { label: string; href: string };
  secondaryCta: { label: string; href: string };
  tertiaryCta?: { label: string; href: string };
  conversionKind: string;
};

export const thankYouFlows: Record<ThankYouFlowKey, ThankYouFlow> = {
  "free-guide": {
    key: "free-guide",
    eyebrow: "Guide Set access",
    title: "Your free Marloth and Kruger guides are open.",
    description: "The guide access request has been received. Use this next step to move from general interest into a practical Marloth/Kruger plan.",
    statusLabel: "Guide lead received",
    nextSteps: [
      "Open the free Guide Set while you are still on the site.",
      "Check your email for the guide link where delivery is available.",
      "Use Trip Finder if you are not sure whether route help, accommodation help or local services matter first.",
    ],
    primaryCta: { label: "Open Guide Set", href: "/free-guides" },
    secondaryCta: { label: "Use Trip Finder", href: "/smart-calculator" },
    tertiaryCta: { label: "View route previews", href: "/route-packs" },
    conversionKind: "free_guide",
  },
  "guide-delivery": {
    key: "guide-delivery",
    eyebrow: "Guide delivery",
    title: "Your guide delivery request has been received.",
    description: "MPC has the delivery request. Keep the planning momentum by checking the route and accommodation steps that usually affect Marloth visitors most.",
    statusLabel: "Guide request received",
    nextSteps: [
      "Check the email address you supplied for the guide link or delivery confirmation.",
      "Review route timing before committing to a long Kruger day.",
      "Ask MPC if your stay dates or group size affect the best starting point.",
    ],
    primaryCta: { label: "View route help", href: "/route-packs" },
    secondaryCta: { label: "Plan my visit", href: "/plan-my-visit" },
    tertiaryCta: { label: "Contact MPC", href: "/contact" },
    conversionKind: "guide_request",
  },
  "route-pack": {
    key: "route-pack",
    eyebrow: "Route planning request",
    title: "Your Kruger route-pack request has been received.",
    description: "MPC has the route request and can now follow up with the correct delivery or payment step. Your next best move is to keep the trip details consistent.",
    statusLabel: "Route lead received",
    nextSteps: [
      "Check your inbox for MPC's reply and delivery/payment instructions where relevant.",
      "Keep your travel month, group size and starting point ready for the final route advice.",
      "Use current SANParks gate times, rules and road notices as the final authority before travel.",
    ],
    primaryCta: { label: "View more route previews", href: "/route-packs" },
    secondaryCta: { label: "Crocodile Bridge help", href: "/free-kruger-help/crocodile-bridge" },
    tertiaryCta: { label: "Ask a planning question", href: "/contact" },
    conversionKind: "route_pack_request",
  },
  "order-pack": {
    key: "order-pack",
    eyebrow: "Planning pack order",
    title: "Your planning-pack request has been received.",
    description: "MPC has the request and can now confirm the next delivery step. Use the links below to keep planning while your request is handled.",
    statusLabel: "Pack request received",
    nextSteps: [
      "Check your email for payment or delivery instructions where available.",
      "Review the free route and arrival help sections while you wait.",
      "Reply with your travel month if MPC needs more context before delivery.",
    ],
    primaryCta: { label: "View route previews", href: "/route-packs" },
    secondaryCta: { label: "Open Guide Set", href: "/free-guides" },
    tertiaryCta: { label: "Contact MPC", href: "/contact" },
    conversionKind: "order_pack_request",
  },
  "stay-enquiry": {
    key: "stay-enquiry",
    eyebrow: "Stay enquiry",
    title: "Your stay enquiry has been received.",
    description: "MPC has your stay details and can reply about availability, fit and next steps. The strongest next action is to keep your dates and visitor group ready.",
    statusLabel: "Stay lead received",
    nextSteps: [
      "Check your email for MPC's reply about availability or the next booking discussion.",
      "Keep your preferred travel dates, visitor group size and monthly-rental interest ready.",
      "Review route and arrival help so the stay enquiry turns into a better trip plan.",
    ],
    primaryCta: { label: "View accommodation", href: "/accommodation" },
    secondaryCta: { label: "Plan my visit", href: "/plan-my-visit" },
    tertiaryCta: { label: "Open Guide Set", href: "/free-guides" },
    conversionKind: "stay_enquiry",
  },
  "accommodation-help": {
    key: "accommodation-help",
    eyebrow: "Accommodation help",
    title: "Your accommodation help request has been received.",
    description: "MPC has your enquiry and can help you narrow down availability, visitor fit, timing and next steps for Marloth Park accommodation.",
    statusLabel: "Accommodation lead received",
    nextSteps: [
      "Check your inbox for MPC's reply or follow-up question.",
      "Keep travel dates, visitor group size, budget range and must-have features ready.",
      "Compare listed accommodation while MPC handles the enquiry path.",
    ],
    primaryCta: { label: "View accommodation", href: "/accommodation" },
    secondaryCta: { label: "View featured stays", href: "/accommodation/featured" },
    tertiaryCta: { label: "Ask another question", href: "/contact" },
    conversionKind: "accommodation_enquiry",
  },
  "business-onboarding": {
    key: "business-onboarding",
    eyebrow: "Business listing request",
    title: "Your business listing request has been received.",
    description: "MPC will review the business details before anything appears publicly. This keeps local categories useful, accurate and visitor-safe.",
    statusLabel: "Business lead received",
    nextSteps: [
      "Keep clear contact details, service area, opening hours and payment options ready.",
      "Prepare one strong landscape image if MPC asks for listing media.",
      "Wait for MPC to confirm category fit, listing option and the next step before any payment is requested.",
    ],
    primaryCta: { label: "Business terms", href: "/supplier-terms" },
    secondaryCta: { label: "View listing options", href: "/business/listing-application" },
    tertiaryCta: { label: "Contact MPC", href: "/contact" },
    conversionKind: "business_signup",
  },
  "listing-onboarding": {
    key: "listing-onboarding",
    eyebrow: "Local details",
    title: "Your local details have been received.",
    description: "MPC will review the details, image quality and enquiry context before anything is published.",
    statusLabel: "Contact lead received",
    nextSteps: [
      "Prepare 6 to 12 clear images or a photo-folder link if relevant.",
      "Keep sleeps, bedrooms, bathrooms, location notes and booking rules ready.",
      "Wait for MPC to confirm the next practical step where relevant.",
    ],
    primaryCta: { label: "View accommodation", href: "/accommodation" },
    secondaryCta: { label: "Contact MPC", href: "/contact" },
    tertiaryCta: { label: "Contact MPC", href: "/contact" },
    conversionKind: "listing_signup",
  },
  contact: {
    key: "contact",
    eyebrow: "Message sent",
    title: "Your message has been sent to MPC.",
    description: "MPC has received your message. Use the next best link below if you want to continue planning while waiting for a reply.",
    statusLabel: "Contact lead received",
    nextSteps: [
      "Check your inbox for MPC's reply.",
      "Use Trip Finder if your question is about where to start.",
      "Use the free Guide Set if you need immediate visitor planning help.",
    ],
    primaryCta: { label: "Use Trip Finder", href: "/smart-calculator" },
    secondaryCta: { label: "Open Guide Set", href: "/free-guides" },
    tertiaryCta: { label: "View accommodation", href: "/accommodation" },
    conversionKind: "contact",
  },
  "trip-finder": {
    key: "trip-finder",
    eyebrow: "Trip Finder",
    title: "Your Trip Finder result has been saved.",
    description: "MPC can use your result as context for follow-up. Your next action is to choose the planning path closest to your trip.",
    statusLabel: "Trip Finder lead received",
    nextSteps: [
      "Use the recommended starting point from your Trip Finder result.",
      "Open Guide Set if you want immediate help without waiting.",
      "Ask MPC if the recommendation does not fit your travel group.",
    ],
    primaryCta: { label: "Plan my visit", href: "/plan-my-visit" },
    secondaryCta: { label: "Open Guide Set", href: "/free-guides" },
    tertiaryCta: { label: "Contact MPC", href: "/contact" },
    conversionKind: "trip_finder",
  },
  lead: {
    key: "lead",
    eyebrow: "Thank you",
    title: "MPC has received your request.",
    description: "Your request has been received. Choose the most useful next planning step below.",
    statusLabel: "Lead received",
    nextSteps: [
      "Check your email for MPC's reply where delivery is available.",
      "Use the free Guide Set for immediate visitor help.",
      "Use Trip Finder if you are not sure what to do next.",
    ],
    primaryCta: { label: "Open Guide Set", href: "/free-guides" },
    secondaryCta: { label: "Use Trip Finder", href: "/smart-calculator" },
    tertiaryCta: { label: "Contact MPC", href: "/contact" },
    conversionKind: "lead",
  },
};

export const thankYouFlowAliases: Record<string, ThankYouFlowKey> = {
  "free_guide": "free-guide",
  "free-guide": "free-guide",
  "guide": "guide-delivery",
  "guide_request": "guide-delivery",
  "guide-delivery": "guide-delivery",
  "route": "route-pack",
  "route_pack": "route-pack",
  "route_pack_request": "route-pack",
  "route-pack": "route-pack",
  "order_pack": "order-pack",
  "order_pack_request": "order-pack",
  "order-pack": "order-pack",
  "stay": "stay-enquiry",
  "stay_enquiry": "stay-enquiry",
  "stay-enquiry": "stay-enquiry",
  "accommodation": "accommodation-help",
  "accommodation_enquiry": "accommodation-help",
  "accommodation-help": "accommodation-help",
  "business": "business-onboarding",
  "business_signup": "business-onboarding",
  "business-onboarding": "business-onboarding",
  "listing_signup": "listing-onboarding",
  "listing-onboarding": "listing-onboarding",
  "contact": "contact",
  "trip_finder": "trip-finder",
  "trip-finder": "trip-finder",
};

export function resolveThankYouFlowKey(value?: string | null): ThankYouFlowKey {
  const key = String(value || "lead").trim().toLowerCase();
  return thankYouFlowAliases[key] || (key in thankYouFlows ? (key as ThankYouFlowKey) : "lead");
}

export function getThankYouFlow(value?: string | null): ThankYouFlow {
  return thankYouFlows[resolveThankYouFlowKey(value)];
}

export function thankYouPathForLeadKind(kind: string, reference?: string | null): string {
  const flowKey = resolveThankYouFlowKey(kind);
  const path = `/thank-you/${flowKey}`;
  const cleanRef = String(reference || "").trim();
  return cleanRef ? `${path}?ref=${encodeURIComponent(cleanRef)}` : path;
}
