import { createFileRoute } from "@tanstack/react-router";
import { useState } from "react";
import { Zap, Shield, QrCode, Crown, Gem, Dices, Star, ShieldCheck, IndianRupee } from "lucide-react";
import { LobbyShell, PageTitle, Steps, useToast, card } from "@/components/lotus/LobbyShell";
import { useBalance, fmt, addBalance } from "@/lib/wallet";

export const Route = createFileRoute("/deposit")({
  head: () => ({
    meta: [
      { title: "Deposit Wallet – Lotus365 Demo" },
      { name: "description", content: "Pick a payment channel and amount to top up your Lotus365 demo wallet." },
      { property: "og:title", content: "Deposit Wallet – Lotus365" },
      { property: "og:description", content: "Top up your demo wallet with ZX, CT or BZ Pay." },
      { property: "og:type", content: "website" },
      { name: "twitter:card", content: "summary" },
    ],
  }),
  component: DepositPage,
});

const CHANNELS = [
  { id: "ZX PAY", sub: "Instant UPI 2.0", tag: "FASTEST", tagCls: "bg-gold text-gold-foreground", Icon: Zap },
  { id: "CT PAY", sub: "Secure High-Pass", tag: "VIP LIMIT", tagCls: "bg-pill-blue text-foreground", Icon: Shield },
  { id: "BZ PAY", sub: "Instant QR Direct", tag: "POPULAR", tagCls: "bg-win text-gold-foreground", Icon: QrCode },
];
const AMOUNTS = [300, 500, 1000, 2000, 5000, 10000];
export const OFFERS = [
  { name: "WELCOME OFFER", dep: 5000, bonus: 2000, Icon: Crown },
  { name: "HIGH ROLLER PACK", dep: 10000, bonus: 5000, Icon: Gem },
  { name: "NOBLE WHEEL PACK", dep: 25000, bonus: 8000, Icon: Dices },
  { name: "REGAL VIP PACK", dep: 50000, bonus: 15000, Icon: Star },
];
const inr = (n: number) => `₹${n.toLocaleString("en-IN")}`;

function DepositPage() {
  const balance = useBalance();
  const [ch, setCh] = useState<string | null>(null);
  const [amt, setAmt] = useState(500);
  const [offer, setOffer] = useState<string | null>(null);
  const toast = useToast();
  const sel = OFFERS.find((o) => o.name === offer);

  const pay = () => {
    if (!ch) return toast.show("Please select a payment channel");
    if (amt < 100) return toast.show("Minimum deposit is ₹100");
    const bonus = sel && amt >= sel.dep ? sel.bonus : 0;
    addBalance(amt + bonus);
    toast.show(`Deposited ${inr(amt)}${bonus ? ` + ${inr(bonus)} bonus` : ""} (demo)`);
  };

  return (
    <LobbyShell>
      {toast.node}
      <PageTitle a="DEPOSIT" b="WALLET" sub="Select channel & amount to top up" right={
        <div className="text-right text-[10px] font-bold text-muted-foreground">WALLET BALANCE<p className="mt-1 flex flex-col items-end text-xs"><IndianRupee className="size-3.5 text-gold" />{fmt(balance)}</p></div>
      } />
      <p className="px-3 pt-4 text-[11px] font-bold text-muted-foreground">SELECT PAYMENT CHANNEL</p>
      <div className="mt-3 grid grid-cols-3 gap-2 px-3">
        {CHANNELS.map(({ id, sub, tag, tagCls, Icon }) => (
          <button key={id} onClick={() => { setCh(id); toast.show(`Selected Channel: ${id}`); }}
            className={`relative ${card} flex flex-col items-center px-1 pb-3 pt-5 transition ${ch === id ? "border-gold shadow-[0_0_12px_var(--gold)]" : ""}`}>
            <span className={`absolute -top-2 rounded px-1.5 py-0.5 text-[8px] font-extrabold ${tagCls}`}>{tag}</span>
            <Icon className="size-5 text-gold" />
            <span className="mt-1.5 text-sm font-extrabold">{id}</span>
            <span className="text-[9px] text-muted-foreground">{sub}</span>
          </button>
        ))}
      </div>
      <section className={`mx-3 mt-4 ${card} p-4`}>
        <p className="text-[11px] font-bold text-muted-foreground">ENTER DEPOSIT AMOUNT</p>
        <label className="mt-3 flex items-center justify-center gap-1 border-b border-border pb-4 text-4xl font-extrabold">
          <span className="text-gold">₹</span>
          <input inputMode="numeric" value={amt || ""} onChange={(e) => setAmt(Number(e.target.value.replace(/\D/g, "").slice(0, 7)))}
            className="w-36 bg-transparent outline-none" aria-label="Deposit amount" />
        </label>
        <div className="mt-4 grid grid-cols-3 gap-2">
          {AMOUNTS.map((a) => (
            <button key={a} onClick={() => setAmt(a)} className={`rounded-md border py-2.5 text-sm font-extrabold transition active:scale-95 ${amt === a ? "border-gold bg-gold text-gold-foreground" : "border-border bg-lobby"}`}>{inr(a)}</button>
          ))}
        </div>
      </section>
      <button onClick={pay} className="mx-3 mt-4 w-[calc(100%-1.5rem)] rounded-lg bg-gold py-3.5 text-sm font-extrabold text-gold-foreground shadow-lg active:scale-[.98]">
        {ch ? "DEPOSIT NOW" : "PROCEED TO PAY"}
      </button>
      <p className="px-3 pt-6 text-[11px] font-bold text-muted-foreground">SELECT BONUS OFFER (OPTIONAL)</p>
      <div className="mt-3 space-y-2.5 px-3">
        {OFFERS.map(({ name, dep, bonus, Icon }) => (
          <button key={name} onClick={() => { setOffer(offer === name ? null : name); setAmt(dep); }}
            className={`flex w-full items-center gap-3 ${card} p-3 text-left ${offer === name ? "border-gold" : ""}`}>
            <span className="grid size-9 place-items-center rounded-md border border-gold/60 bg-gold/10"><Icon className="size-4 text-gold" /></span>
            <span className="flex-1"><b className="block text-xs font-extrabold">{name}</b><span className="text-[10px] text-muted-foreground">Deposit {inr(dep)} Get {inr(bonus)} Bonus</span></span>
            <span className="rounded bg-gold px-2 py-1 text-[11px] font-extrabold text-gold-foreground">+{inr(bonus)}</span>
          </button>
        ))}
      </div>
      <p className="mt-5 flex items-center justify-center gap-1.5 text-xs font-bold text-gold"><ShieldCheck className="size-4" /> 100% SECURE & INSTANT PAYMENT</p>
      <Steps title="HOW TO DEPOSIT?"
        items={[<>Select payment channel (ZX/CT/BZ), amount, and click on <b className="text-foreground">DEPOSIT NOW</b>.</>, "Scan the payment QR code displayed on screen or choose your UPI App.", "Complete payment using any UPI App (GPay/PhonePe/Paytm).", "Funds and applicable bonus will reflect in your wallet balance automatically."]}
        hiItems={[<>पेमेंट चैनल (ZX/CT/BZ) और राशि चुनें, फिर <b className="text-foreground">DEPOSIT NOW</b> पर क्लिक करें।</>, "स्क्रीन पर दिख रहा QR कोड स्कैन करें या अपना UPI ऐप चुनें।", "किसी भी UPI ऐप (GPay/PhonePe/Paytm) से पेमेंट पूरा करें।", "राशि और बोनस आपके वॉलेट बैलेंस में अपने आप जुड़ जाएगा।"]} />
    </LobbyShell>
  );
}
