/**
 * Donation tier configuration.
 *
 * EDIT PRICES HERE.
 *
 * Each tier has:
 *   - id: stable identifier referenced from regions.ts
 *   - label: short human-readable name
 *   - price: amount in CENTS (Stripe units)
 *   - description: explainer copy shown in UI
 *   - opacity: alpha used when rendering the filled overlay (0-1)
 *
 * Per-region price overrides are supported via `regionOverridesCents` below.
 * Example: to make region "wall-row5-brick17" cost $200 (instead of the row
 * default), add: { "wall-row5-brick17": 20000 }
 */

export type TierId =
  | "foundation"
  | "wall-row1"
  | "wall-row2"
  | "wall-row3"
  | "wall-row4"
  | "wall-row5"
  | "wall-row6"
  | "wall-row7"
  | "porch-board"
  | "porch-column"
  | "window-ground"
  | "window-upper"
  | "door"
  | "door-sidelight"
  | "roof-shingle"
  | "roof-gable"
  | "naming-rights";

export interface Tier {
  id: TierId;
  label: string;
  priceCents: number;
  description: string;
  opacity: number;
}

export const TIERS: Record<TierId, Tier> = {
  foundation: {
    id: "foundation",
    label: "Foundation stone",
    priceCents: 500,
    description: "A stone in the foundation course.",
    opacity: 0.55,
  },
  "wall-row1": {
    id: "wall-row1",
    label: "Wall brick (row 1)",
    priceCents: 2500,
    description: "A brick in the lowest course of the wall.",
    opacity: 0.55,
  },
  "wall-row2": {
    id: "wall-row2",
    label: "Wall brick (row 2)",
    priceCents: 3500,
    description: "A brick in the second course of the wall.",
    opacity: 0.55,
  },
  "wall-row3": {
    id: "wall-row3",
    label: "Wall brick (row 3)",
    priceCents: 5000,
    description: "A brick in the third course of the wall.",
    opacity: 0.55,
  },
  "wall-row4": {
    id: "wall-row4",
    label: "Wall brick (row 4)",
    priceCents: 7500,
    description: "A brick in the fourth course of the wall.",
    opacity: 0.55,
  },
  "wall-row5": {
    id: "wall-row5",
    label: "Wall brick (row 5)",
    priceCents: 10000,
    description: "A brick in the fifth course of the wall.",
    opacity: 0.55,
  },
  "wall-row6": {
    id: "wall-row6",
    label: "Wall brick (row 6)",
    priceCents: 15000,
    description: "A brick in the sixth course of the wall.",
    opacity: 0.55,
  },
  "wall-row7": {
    id: "wall-row7",
    label: "Gable wall brick",
    priceCents: 20000,
    description: "A brick in the upper gable wall.",
    opacity: 0.55,
  },
  "porch-board": {
    id: "porch-board",
    label: "Porch board",
    priceCents: 50000,
    description: "A decking board on the front porch.",
    opacity: 0.55,
  },
  "porch-column": {
    id: "porch-column",
    label: "Porch column",
    priceCents: 500000,
    description: "A structural column on the front porch.",
    opacity: 0.55,
  },
  "window-ground": {
    id: "window-ground",
    label: "Ground-floor window",
    priceCents: 500000,
    description: "A ground-floor window.",
    opacity: 0.55,
  },
  "window-upper": {
    id: "window-upper",
    label: "Upper-floor window",
    priceCents: 500000,
    description: "An upper-floor window.",
    opacity: 0.55,
  },
  door: {
    id: "door",
    label: "Front door",
    priceCents: 1000000,
    description: "The front door of the house.",
    opacity: 0.6,
  },
  "door-sidelight": {
    id: "door-sidelight",
    label: "Door sidelight",
    priceCents: 250000,
    description: "A glass sidelight beside the front door.",
    opacity: 0.55,
  },
  "roof-shingle": {
    id: "roof-shingle",
    label: "Roof shingle row",
    priceCents: 100000,
    description: "A row of roof shingles.",
    opacity: 0.55,
  },
  "roof-gable": {
    id: "roof-gable",
    label: "Gable peak shingle",
    priceCents: 250000,
    description: "A shingle on the central gable peak.",
    opacity: 0.55,
  },
  "naming-rights": {
    id: "naming-rights",
    label: "Naming rights (entire house)",
    priceCents: 5000000,
    description:
      "Naming rights for the entire house. One-time, exclusive. A banner displays your name above the structure permanently.",
    opacity: 0.0,
  },
};

/**
 * Per-region price overrides (in CENTS).
 * Lookup precedence: regionOverridesCents[regionId] > TIERS[tierId].priceCents
 *
 * Example:
 *   "wall-row5-brick17": 20000  // make this specific brick cost $200
 */
export const regionOverridesCents: Record<string, number> = {};

export function priceForRegion(regionId: string, tierId: TierId): number {
  const override = regionOverridesCents[regionId];
  if (typeof override === "number") return override;
  return TIERS[tierId].priceCents;
}

export function formatUSD(cents: number): string {
  const dollars = cents / 100;
  if (Number.isInteger(dollars)) {
    return `$${dollars.toLocaleString("en-US")}`;
  }
  return `$${dollars.toLocaleString("en-US", {
    minimumFractionDigits: 2,
    maximumFractionDigits: 2,
  })}`;
}
