import { useState } from "react";
import { useApp } from "../AppContext";
import { BackHeader, Card, cn } from "../components/ui";

interface Method {
  id: string;
  name: string;
  icon: string;
  hint: string;
}

const METHODS: Method[] = [
  { id: "mada", name: "مدى", icon: "ri-bank-card-line", hint: "بطاقات مدى البنكية" },
  { id: "apple", name: "Apple Pay", icon: "ri-apple-fill", hint: "الدفع السريع عبر آبل" },
  { id: "visa", name: "Visa / MasterCard", icon: "ri-visa-line", hint: "بطاقات الائتمان الدولية" },
  { id: "tabby", name: "تابي", icon: "ri-calendar-check-line", hint: "قسّمها على 4 دفعات" },
  { id: "tamara", name: "تمارا", icon: "ri-hand-coin-line", hint: "ادفع لاحقًا بدون فوائد" },
];

export default function Payment() {
  const { goBack, navigate } = useApp();
  const [method, setMethod] = useState("mada");
  const [card, setCard] = useState({ number: "", name: "", expiry: "", cvv: "" });
  const [paying, setPaying] = useState(false);

  const isCard = method === "mada" || method === "visa";
  const total = 1908;

  const pay = () => {
    setPaying(true);
    setTimeout(() => {
      setPaying(false);
      navigate("paymentSuccess");
    }, 1200);
  };

  return (
    <div className="min-h-full flex flex-col">
      <BackHeader title="الدفع" onBack={goBack} />

      <div className="px-4 py-4 space-y-5">
        <div className="rounded-[20px] bg-primary-500 p-5 text-background-50">
          <p className="text-[12.5px] text-background-50/70">الإجمالي المطلوب دفعه</p>
          <p className="text-[30px] font-bold mt-1">
            {total.toLocaleString("en-US")} <span className="text-[17px] font-semibold">ر.س</span>
          </p>
          <p className="text-[11px] text-background-50/60 mt-1">شامل الضريبة وتكلفة الشحن</p>
        </div>

        <div>
          <h2 className="text-[15px] font-semibold text-foreground-950 mb-3">طريقة الدفع</h2>
          <div className="space-y-2.5">
            {METHODS.map((m) => {
              const active = method === m.id;
              return (
                <button
                  key={m.id}
                  type="button"
                  onClick={() => setMethod(m.id)}
                  className={cn(
                    "w-full flex items-center gap-3.5 rounded-[18px] border p-3.5 cursor-pointer transition-colors text-right",
                    active ? "bg-background-100 border-foreground-950" : "bg-background-100 border-background-200 hover:border-foreground-300",
                  )}
                >
                  <span className={cn("w-5 h-5 rounded-full border-2 flex items-center justify-center shrink-0", active ? "border-foreground-950" : "border-background-400")}>
                    {active && <span className="w-2.5 h-2.5 rounded-full bg-foreground-950" />}
                  </span>
                  <div className="w-11 h-11 rounded-[14px] bg-background-200 flex items-center justify-center shrink-0">
                    <i className={cn(m.icon, "text-[22px] text-foreground-950 leading-none")} />
                  </div>
                  <div className="flex-1 min-w-0">
                    <p className="text-[13.5px] font-semibold text-foreground-950">{m.name}</p>
                    <p className="text-[11px] text-foreground-600">{m.hint}</p>
                  </div>
                </button>
              );
            })}
          </div>
        </div>

        {isCard && (
          <Card className="space-y-4 animate-fade-in">
            <h2 className="text-[15px] font-semibold text-foreground-950">بيانات البطاقة</h2>
            <div>
              <label className="text-[12px] font-medium text-foreground-800 block mb-1.5">رقم البطاقة</label>
              <div className="flex items-center h-[52px] rounded-[14px] bg-background-100 border border-background-200 px-4" dir="ltr">
                <i className="ri-bank-card-line text-[18px] text-foreground-500 leading-none mr-2" />
                <input
                  value={card.number}
                  onChange={(e) => setCard({ ...card, number: e.target.value })}
                  placeholder="0000 0000 0000 0000"
                  inputMode="numeric"
                  className="flex-1 bg-transparent text-[14px] text-foreground-950 placeholder:text-foreground-500 outline-none font-body tracking-wider"
                />
              </div>
            </div>
            <div>
              <label className="text-[12px] font-medium text-foreground-800 block mb-1.5">اسم حامل البطاقة</label>
              <input
                value={card.name}
                onChange={(e) => setCard({ ...card, name: e.target.value })}
                placeholder="ABDULLAH ALHARBI"
                className="w-full h-[52px] rounded-[14px] bg-background-100 border border-background-200 px-4 text-[14px] text-foreground-950 placeholder:text-foreground-500 outline-none focus:border-foreground-400 font-body"
              />
            </div>
            <div className="grid grid-cols-2 gap-3">
              <div>
                <label className="text-[12px] font-medium text-foreground-800 block mb-1.5">تاريخ الانتهاء</label>
                <input
                  value={card.expiry}
                  onChange={(e) => setCard({ ...card, expiry: e.target.value })}
                  placeholder="MM / YY"
                  className="w-full h-[52px] rounded-[14px] bg-background-100 border border-background-200 px-4 text-[14px] text-foreground-950 placeholder:text-foreground-500 outline-none focus:border-foreground-400 font-body"
                />
              </div>
              <div>
                <label className="text-[12px] font-medium text-foreground-800 block mb-1.5">CVV</label>
                <input
                  value={card.cvv}
                  onChange={(e) => setCard({ ...card, cvv: e.target.value.replace(/\D/g, "").slice(0, 4) })}
                  placeholder="123"
                  inputMode="numeric"
                  className="w-full h-[52px] rounded-[14px] bg-background-100 border border-background-200 px-4 text-[14px] text-foreground-950 placeholder:text-foreground-500 outline-none focus:border-foreground-400 font-body"
                />
              </div>
            </div>
          </Card>
        )}
      </div>

      <div className="sticky bottom-0 z-30 mt-auto p-4 bg-background-50/95 backdrop-blur border-t border-background-200/70">
        <button
          type="button"
          onClick={pay}
          disabled={paying}
          className="w-full h-[52px] rounded-[14px] bg-primary-500 text-background-50 text-[15px] font-semibold cursor-pointer active:scale-[0.98] transition-transform disabled:opacity-50"
        >
          {paying ? "جاري تنفيذ الدفع..." : `ادفع الآن ${total.toLocaleString("en-US")} ر.س`}
        </button>
        <p className="text-[10.5px] text-foreground-500 text-center mt-2.5">
          لا يمكن إلغاء الطلب بعد الدفع
        </p>
        <button
          type="button"
          onClick={() => navigate("paymentSuccess", { orderId: "fail" })}
          className="w-full text-center text-[11px] text-foreground-400 mt-1 cursor-pointer"
        >
          عرض حالة فشل الدفع (تجريبي)
        </button>
      </div>
    </div>
  );
}