/**
 * SINGLE SOURCE OF TRUTH for every loyalty value in KareVoyage.
 *
 * Admin CMS and the customer-facing app must both read from this config.
 * Never hard-code a tier threshold, bonus or expiry anywhere else in the UI.
 */

export interface LoyaltyTierConfig {
  id: string;
  name: "Gold" | "Platinum" | "Infinity";
  qualifyingTrips: number;
  qualifyingPoints: number;
  earnRatePct: number;
  benefits: string;
  sortOrder: number;
  effectiveDate: string;
  status: "Active" | "Inactive";
}

export const LOYALTY_CONFIG = {
  currency: "INR",
  /** Points credited once, when a guest registers. */
  joiningBonusPoints: 2000,
  /** Points credited when a referred guest completes travel. */
  referralRewardPoints: 5000,
  /** 1 point = ₹1 at redemption. */
  pointValueInr: 1,
  /** Maximum share of net payable that may be settled with points. */
  maxRedemptionPct: 20,
  /** Months before an earned point expires. */
  pointExpiryMonths: 12,
  /** Base earn rates by tour scope, before tier uplift. */
  earnRates: {
    domesticPct: 1.5,
    internationalPct: 3,
    fitPct: 1,
  },
  tiers: [
    {
      id: "TIER-1",
      name: "Gold",
      qualifyingTrips: 1,
      qualifyingPoints: 10000,
      earnRatePct: 2,
      benefits: "Priority support, 2% reward rate",
      sortOrder: 1,
      effectiveDate: "2026-01-01",
      status: "Active",
    },
    {
      id: "TIER-2",
      name: "Platinum",
      qualifyingTrips: 3,
      qualifyingPoints: 30000,
      earnRatePct: 4,
      benefits: "Room upgrade priority, 4% reward rate",
      sortOrder: 2,
      effectiveDate: "2026-01-01",
      status: "Active",
    },
    {
      id: "TIER-3",
      name: "Infinity",
      qualifyingTrips: 6,
      qualifyingPoints: 75000,
      earnRatePct: 6,
      benefits: "Dedicated journey manager, 6% reward rate, lounge access",
      sortOrder: 3,
      effectiveDate: "2026-01-01",
      status: "Active",
    },
  ] satisfies LoyaltyTierConfig[],
} as const;

export const formatPoints = (n: number) => `${n.toLocaleString("en-IN")} pts`;

/** Human-readable rule rows, generated from the config so nothing drifts. */
export const loyaltyRules = [
  {
    id: "RR-1",
    rule: "Joining Bonus",
    value: `${LOYALTY_CONFIG.joiningBonusPoints.toLocaleString("en-IN")} points`,
    appliesTo: "New guest registration",
    expiryMonths: LOYALTY_CONFIG.pointExpiryMonths,
    status: "Active" as const,
  },
  {
    id: "RR-2",
    rule: "Domestic Tour Reward",
    value: `${LOYALTY_CONFIG.earnRates.domesticPct}% of trip value`,
    appliesTo: "Completed domestic tours",
    expiryMonths: LOYALTY_CONFIG.pointExpiryMonths,
    status: "Active" as const,
  },
  {
    id: "RR-3",
    rule: "International Tour Reward",
    value: `${LOYALTY_CONFIG.earnRates.internationalPct}% of trip value`,
    appliesTo: "Completed international tours",
    expiryMonths: LOYALTY_CONFIG.pointExpiryMonths,
    status: "Active" as const,
  },
  {
    id: "RR-4",
    rule: "FIT Reward",
    value: `${LOYALTY_CONFIG.earnRates.fitPct}% of trip value`,
    appliesTo: "Independent travel bookings",
    expiryMonths: LOYALTY_CONFIG.pointExpiryMonths,
    status: "Active" as const,
  },
  {
    id: "RR-5",
    rule: "Referral Reward",
    value: `${LOYALTY_CONFIG.referralRewardPoints.toLocaleString("en-IN")} points`,
    appliesTo: "Referred guest completes travel",
    expiryMonths: LOYALTY_CONFIG.pointExpiryMonths,
    status: "Active" as const,
  },
  {
    id: "RR-6",
    rule: "Redemption Rule",
    value: `1 point = ₹${LOYALTY_CONFIG.pointValueInr}, max ${LOYALTY_CONFIG.maxRedemptionPct}% of net payable`,
    appliesTo: "All bookings",
    expiryMonths: 0,
    status: "Active" as const,
  },
  {
    id: "RR-7",
    rule: "Points Expiry",
    value: `${LOYALTY_CONFIG.pointExpiryMonths} months from credit date`,
    appliesTo: "All earned points",
    expiryMonths: LOYALTY_CONFIG.pointExpiryMonths,
    status: "Active" as const,
  },
  {
    id: "RR-8",
    rule: "Manual Adjustment",
    value: "Admin defined",
    appliesTo: "Support resolutions",
    expiryMonths: LOYALTY_CONFIG.pointExpiryMonths,
    status: "Active" as const,
  },
];
