"use client";

import { useMemo, useState } from "react";

type Category = "casino" | "sports";
type Brand = {
  id: string;
  name: string;
  monogram: string;
  categories: Category[];
  promo: string;
  bonus: string;
  about: string;
  verified: string;
  accent: string;
};

const brands: Brand[] = [
  {
    id: "baji",
    name: "Baji",
    monogram: "BJ",
    categories: ["casino", "sports"],
    promo: "BAJI-CODE",
    bonus: "Verified offer",
    about: "Current English and Bangla offer description will be managed from Admin after source verification.",
    verified: "Review required",
    accent: "lime",
  },
  {
    id: "jezzbuzz",
    name: "JezzBuzz",
    monogram: "JZ",
    categories: ["casino"],
    promo: "JZ-CODE",
    bonus: "Verified offer",
    about: "Offer conditions, promo code and evidence date are loaded from the central Offer Master.",
    verified: "Review required",
    accent: "violet",
  },
  {
    id: "mcw",
    name: "MCW",
    monogram: "MCW",
    categories: ["casino", "sports"],
    promo: "MCW-CODE",
    bonus: "Verified offer",
    about: "Casino and sports details appear here with the latest verified operator terms.",
    verified: "Review required",
    accent: "amber",
  },
];

function ArrowIcon() {
  return <svg viewBox="0 0 24 24" aria-hidden="true"><path d="M7 17 17 7M9 7h8v8" /></svg>;
}

function CopyIcon() {
  return <svg viewBox="0 0 24 24" aria-hidden="true"><rect x="8" y="8" width="11" height="11" rx="2" /><path d="M16 8V6a2 2 0 0 0-2-2H6a2 2 0 0 0-2 2v8a2 2 0 0 0 2 2h2" /></svg>;
}

export default function Home() {
  const [filter, setFilter] = useState<"all" | Category>("all");
  const [selected, setSelected] = useState<Brand | null>(brands[0]);
  const [notice, setNotice] = useState("Tap a brand to inspect the offer.");
  const visible = useMemo(() => brands.filter((b) => filter === "all" || b.categories.includes(filter)), [filter]);

  async function copyPromo(brand: Brand) {
    try { await navigator.clipboard.writeText(brand.promo); } catch {}
    setNotice(`Copied ${brand.promo}`);
  }

  function play(brand: Brand) {
    setNotice(`PLAY ${brand.name}: Admin URL → click ID → SubID → affiliate website`);
  }

  return (
    <main className="app-page">
      <section className="app-shell">
        <header className="app-header">
          <div className="identity">
            <div className="flag" aria-hidden="true">🇧🇩</div>
            <div><p className="eyebrow">BANGLADESH</p><h1>BD Offers</h1><p>Casino & sports offers · 18+</p></div>
          </div>
          <a className="admin-link" href="/admin">Admin</a>
        </header>

        <div className="category-tabs" role="tablist" aria-label="Offer categories">
          {(["all", "casino", "sports"] as const).map((item) => (
            <button key={item} className={filter === item ? "active" : ""} onClick={() => setFilter(item)} role="tab" aria-selected={filter === item}>
              {item === "all" ? "All offers" : item === "casino" ? "Casino" : "Sports"}
            </button>
          ))}
        </div>

        <div className="offer-grid">
          {visible.map((brand) => (
            <article className={`offer-card accent-${brand.accent}`} key={brand.id} onClick={() => setSelected(brand)}>
              <div className="offer-main">
                <div className="brand-logo">{brand.monogram}</div>
                <div className="brand-copy">
                  <div className="badges"><span>{brand.bonus}</span>{brand.categories.map((c) => <span key={c}>{c}</span>)}</div>
                  <h2>{brand.name}</h2>
                </div>
                <button className="arrow-button" aria-label={`View ${brand.name}`} onClick={(e) => { e.stopPropagation(); setSelected(brand); }}><ArrowIcon /></button>
              </div>
              <div className="promo-strip"><span>PROMO</span><strong>{brand.promo}</strong><button onClick={(e) => { e.stopPropagation(); copyPromo(brand); }}><CopyIcon /> Copy</button></div>
            </article>
          ))}
        </div>

        <p className="notice" aria-live="polite">{notice}</p>
        <footer><span>18+ only</span><span>Terms apply</span><span>Gamble responsibly</span><span>Affiliate disclosure</span></footer>
      </section>

      {selected && (
        <div className="modal-backdrop" onMouseDown={(e) => { if (e.currentTarget === e.target) setSelected(null); }}>
          <section className="detail-modal" role="dialog" aria-modal="true" aria-labelledby="brand-title">
            <div className="modal-grip" />
            <header className="modal-header">
              <div className="identity"><div className={`brand-logo accent-${selected.accent}`}>{selected.monogram}</div><div><div className="badges"><span>{selected.bonus}</span>{selected.categories.map((c) => <span key={c}>{c}</span>)}</div><h2 id="brand-title">{selected.name}</h2></div></div>
              <button className="close-button" onClick={() => setSelected(null)} aria-label="Close">×</button>
            </header>
            <div className="detail-block"><span>ABOUT</span><p>{selected.about}</p><small>Last checked: {selected.verified}</small></div>
            <div className="detail-block promo-detail"><span>PROMO</span><div><strong>{selected.promo}</strong><button onClick={() => copyPromo(selected)}><CopyIcon /> Copy</button></div></div>
            <button className="play-button" onClick={() => play(selected)}>PLAY <ArrowIcon /></button>
            <div className="other-brands"><span>OTHER BRANDS</span><div>{brands.filter((b) => b.id !== selected.id).map((brand) => <button key={brand.id} onClick={() => setSelected(brand)}><i>{brand.monogram}</i><span>{brand.name}<small>Open details</small></span><ArrowIcon /></button>)}</div></div>
          </section>
        </div>
      )}
    </main>
  );
}
