import { useAuthStore } from "../../stores/authStore";
import firstTrophy from "../../assets/icons/first_trophy.png";
import secondTrophy from "../../assets/icons/second_trophy.png";
import thirdTrophy from "../../assets/icons/third_trophy.png";
import { useEffect, useState } from "react";
import { formatTimeFilter, getRandomColor } from "../../lib/utils";
import {
  Avatar,
  AvatarFallback,
  AvatarImage,
} from "../../components/ui/avatar";
import { Flame, LucideBadgeQuestionMark, Zap, ChevronDown } from "lucide-react";
import type { Leaderboard } from "../../types/leaderboard";
import { api } from "../../utils/api";
import { LeaderboardRowSkeleton } from "../../components/LeaderboardSkeleton";
import { useExamConfigStore } from "../../stores/useExamConfigStore";
import {
  DropdownMenu,
  DropdownMenuContent,
  DropdownMenuRadioGroup,
  DropdownMenuRadioItem,
  DropdownMenuTrigger,
} from "../../components/ui/dropdown-menu";

const DOTS = [
  { size: 10, color: "#f97316", top: "6%", left: "4%", delay: "0s" },
  { size: 7, color: "#a855f7", top: "28%", left: "2%", delay: "1.2s" },
  { size: 9, color: "#22c55e", top: "58%", left: "3%", delay: "0.6s" },
  { size: 12, color: "#3b82f6", top: "10%", right: "4%", delay: "1.8s" },
  { size: 7, color: "#f43f5e", top: "42%", right: "2%", delay: "0.9s" },
  { size: 9, color: "#eab308", top: "72%", right: "5%", delay: "0.4s" },
];

const STYLES = `
  @import url('https://fonts.googleapis.com/css2?family=Nunito:wght@400;700;800;900&family=Nunito+Sans:wght@400;600;700&display=swap');

  .rw-screen {
    height: 100vh;
    background: #fffbf4;
    font-family: 'Nunito', sans-serif;
    position: relative;
    display: flex;
    flex-direction: column;
    overflow: hidden;
    padding-bottom: 0;
  }

  .rw-blob { position: fixed; pointer-events: none; z-index: 0; filter: blur(48px); opacity: 0.35; }
  .rw-blob-1 { width:240px;height:240px;background:#fde68a;top:-80px;left:-80px;border-radius:60% 40% 70% 30%/50% 60% 40% 50%;animation:rwWobble1 14s ease-in-out infinite; }
  .rw-blob-2 { width:190px;height:190px;background:#a5f3c0;bottom:-50px;left:6%;border-radius:40% 60% 30% 70%/60% 40% 60% 40%;animation:rwWobble2 16s ease-in-out infinite; }
  .rw-blob-3 { width:210px;height:210px;background:#fbcfe8;top:15%;right:-60px;border-radius:70% 30% 50% 50%/40% 60% 40% 60%;animation:rwWobble1 18s ease-in-out infinite reverse; }
  .rw-blob-4 { width:150px;height:150px;background:#bfdbfe;bottom:12%;right:2%;border-radius:50% 50% 30% 70%/60% 40% 60% 40%;animation:rwWobble2 12s ease-in-out infinite; }

  @keyframes rwWobble1 {
    0%,100%{border-radius:60% 40% 70% 30%/50% 60% 40% 50%;transform:translate(0,0) rotate(0deg);}
    50%{border-radius:40% 60% 30% 70%/60% 40% 60% 40%;transform:translate(12px,16px) rotate(8deg);}
  }
  @keyframes rwWobble2 {
    0%,100%{border-radius:40% 60% 30% 70%/60% 40% 60% 40%;transform:translate(0,0) rotate(0deg);}
    50%{border-radius:60% 40% 70% 30%/40% 60% 40% 60%;transform:translate(-10px,12px) rotate(-6deg);}
  }

  .rw-dot { position:fixed;border-radius:50%;pointer-events:none;z-index:0;opacity:0.3;animation:rwFloat 7s ease-in-out infinite; }
  @keyframes rwFloat {
    0%,100%{transform:translateY(0) rotate(0deg);}
    50%{transform:translateY(-12px) rotate(180deg);}
  }

  /* Sticky top wrapper */
  .rw-sticky-top {
    position: relative; z-index: 2;
    background: #fffbf4;
    flex-shrink: 0;
    padding: 2rem 1.25rem 0;
  }
  .rw-sticky-top-inner {
    max-width: 580px; margin: 0 auto;
    display: flex; flex-direction: column; gap: 1.25rem;
    padding-bottom: 1rem;
    border-bottom: 2px solid #f3f4f6;
  }

  /* Scrollable list area */
  .rw-scroll-area {
    position: relative; z-index: 1;
    flex: 1; overflow-y: auto;
     padding: 1rem 1.25rem 10rem;
    -webkit-overflow-scrolling: touch;
  }
  .rw-scroll-inner {
    max-width: 580px; margin: 0 auto;
  }

  @keyframes rwPopIn {
    from { opacity:0; transform: scale(0.92) translateY(12px); }
    to   { opacity:1; transform: scale(1) translateY(0); }
  }
  .rw-anim   { animation: rwPopIn 0.4s cubic-bezier(0.34,1.56,0.64,1) both; }
  .rw-anim-1 { animation-delay: 0.05s; }
  .rw-anim-2 { animation-delay: 0.1s; }

  /* Header */
  .rw-header { display:flex;flex-direction:column;align-items:center;gap:0.4rem;text-align:center; }
  .rw-title  { font-size:1.9rem;font-weight:900;color:#1e1b4b;letter-spacing:-0.02em; }
  .rw-rank-text { font-family:'Nunito Sans',sans-serif;font-size:0.9rem;font-weight:600;color:#9ca3af; }
  .rw-rank-text span { color:#a855f7;font-weight:800; }

  /* Controls row */
  .rw-controls {
    display: flex; align-items: center; justify-content: space-between; gap: 0.5rem;
  }

  /* Tab pills */
  .rw-tabs { display:flex;gap:0.4rem; }
  .rw-tab-btn {
    padding: 0.45rem 0.9rem; border-radius: 100px; border: none; cursor: pointer;
    font-family: 'Nunito', sans-serif; font-size: 0.78rem; font-weight: 800;
    display: flex; align-items: center; gap: 0.35rem;
    transition: all 0.2s ease;
    background: white; border: 2.5px solid #f3f4f6; color: #9ca3af;
    box-shadow: 0 2px 8px rgba(0,0,0,0.04);
  }
  .rw-tab-btn.active { background:#1e1b4b; border-color:#1e1b4b; color:white; box-shadow:0 4px 0 #1e1b4b55; }

  /* Time filter button */
  .rw-filter-btn {
    display: flex; align-items: center; gap: 0.35rem;
    padding: 0.45rem 0.9rem; border-radius: 100px; cursor: pointer;
    font-family: 'Nunito', sans-serif; font-size: 0.78rem; font-weight: 800;
    background: white; border: 2.5px solid #f3f4f6; color: #6b7280;
    box-shadow: 0 2px 8px rgba(0,0,0,0.04);
    transition: border-color 0.2s;
  }
  .rw-filter-btn:hover { border-color: #c4b5fd; color: #7c3aed; }

  /* Leaderboard list */
  .rw-list { display:flex;flex-direction:column;gap:0.6rem; }

  /* Each row */
  .rw-row {
    display: flex; align-items: center; justify-content: space-between;
    background: white; border: 2.5px solid #f3f4f6; border-radius: 18px;
    padding: 0.7rem 1rem;
    box-shadow: 0 3px 10px rgba(0,0,0,0.04);
    transition: transform 0.15s ease, box-shadow 0.15s ease;
  }
  .rw-row:hover { transform:translateY(-1px); box-shadow:0 6px 16px rgba(0,0,0,0.07); }
  .rw-row.top-1 { border-color:#fde68a; background:linear-gradient(135deg,#fffbeb,white); }
  .rw-row.top-2 { border-color:#e2e8f0; background:linear-gradient(135deg,#f8fafc,white); }
  .rw-row.top-3 { border-color:#fecba8; background:linear-gradient(135deg,#fff7ed,white); }

  .rw-row-left  { display:flex;align-items:center;gap:0.75rem; }
  .rw-rank-cell { width:36px;display:flex;align-items:center;justify-content:center; }
  .rw-rank-num  { font-size:0.9rem;font-weight:900;color:#9ca3af; }
  .rw-user-name { font-size:0.88rem;font-weight:800;color:#1e1b4b; }
  .rw-row-right { display:flex;align-items:center;gap:0.35rem; }
  .rw-score     { font-size:0.9rem;font-weight:900;color:#1e1b4b; }

  /* XP chip color variants */
  .rw-xp-chip   { color:#84cc16; }
  .rw-q-chip    { color:#0891b2; }
  .rw-fire-chip { color:#ef4444; }

  /* Skeleton */
  .rw-skeleton { display:flex;flex-direction:column;gap:0.6rem; }

  /* ── Floating island you-pill ── */
  .rw-island-wrap {
    position: fixed;
    bottom: calc(1.25rem + 80px + env(safe-area-inset-bottom));
    left: 50%;
    transform: translateX(-50%);
    z-index: 20;
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 0.5rem;
    max-width: 480px
  }

  /* Expanded info card — slides up */
  .rw-island-card {
    background: rgba(255,251,244,0.92);
    backdrop-filter: blur(20px);
    -webkit-backdrop-filter: blur(20px);
    border: 1.5px solid rgba(255,255,255,0.8);
    border-radius: 24px;
    box-shadow: 0 8px 32px rgba(0,0,0,0.12), inset 0 1px 0 rgba(255,255,255,0.9);
    padding: 1rem 1.25rem;
    display: grid;
    grid-template-columns: 1fr 1fr 1fr;
    gap: 0.5rem;
    min-width: 350px;

    /* Hidden by default */
    opacity: 0;
    transform: translateY(12px) scale(0.95);
    pointer-events: none;
    transition:
      opacity 0.3s cubic-bezier(0.34,1.56,0.64,1),
      transform 0.35s cubic-bezier(0.34,1.56,0.64,1);
  }
  .rw-island-card.open {
    opacity: 1;
    transform: translateY(0) scale(1);
    pointer-events: auto;
  }

  .rw-island-stat {
    display: flex; flex-direction: column; align-items: center; gap: 0.2rem;
    padding: 0.5rem 0.4rem;
    background: white; border: 2px solid #f3f4f6; border-radius: 14px;
  }
  .rw-island-stat-val {
    font-family: 'Nunito', sans-serif;
    font-size: 1rem; font-weight: 900; color: #1e1b4b; line-height: 1;
  }
  .rw-island-stat-label {
    font-family: 'Nunito', sans-serif;
    font-size: 0.58rem; font-weight: 800; letter-spacing: 0.1em;
    text-transform: uppercase; color: #9ca3af;
  }
  .rw-island-stat.xp   { border-color: #d9f99d; }
  .rw-island-stat.xp   .rw-island-stat-val { color: #16a34a; }
  .rw-island-stat.rank { border-color: #e9d5ff; }
  .rw-island-stat.rank .rw-island-stat-val { color: #9333ea; }
  .rw-island-stat.lvl  { border-color: #bfdbfe; }
  .rw-island-stat.lvl  .rw-island-stat-val { color: #2563eb; }

  /* The pill button itself */
  .rw-island-pill {
  
    display: flex; align-items: center; gap: 0.65rem;
    background: linear-gradient(135deg, #7c3aed, #a855f7);
    border: 1.5px solid rgba(255,255,255,0.25);
    border-radius: 100px;
    padding: 0.45rem 1rem 0.45rem 0.45rem;
    box-shadow:
      0 6px 20px rgba(124,58,237,0.35),
      0 2px 6px rgba(124,58,237,0.2),
      inset 0 1px 0 rgba(255,255,255,0.2);
    cursor: pointer;
    user-select: none;
    -webkit-tap-highlight-color: transparent;
    transition: transform 0.2s cubic-bezier(0.34,1.56,0.64,1), box-shadow 0.2s ease;
  }
  .rw-island-pill:active { transform: scale(0.93); }
  .rw-island-pill.open {
    box-shadow:
      0 10px 28px rgba(124,58,237,0.4),
      0 4px 10px rgba(124,58,237,0.25),
      inset 0 1px 0 rgba(255,255,255,0.2);
  }

  .rw-island-avatar {
    width: 38px; height: 38px; border-radius: 50%;
    background: rgba(255,255,255,0.25);
    border: 2px solid rgba(255,255,255,0.4);
    display: flex; align-items: center; justify-content: center;
    font-family: 'Nunito', sans-serif;
    font-size: 0.9rem; font-weight: 900; color: white;
    overflow: hidden; flex-shrink: 0;
  }
  .rw-island-avatar img { width:100%;height:100%;object-fit:cover; }

  .rw-island-info { display:flex;flex-direction:column;gap:1px; }
  .rw-island-you {
    font-family: 'Nunito', sans-serif;
    font-size: 0.58rem; font-weight: 800; letter-spacing:0.12em;
    text-transform: uppercase; color: rgba(255,255,255,0.6);
    line-height: 1;
  }
  .rw-island-name {
    font-family: 'Nunito', sans-serif;
    font-size: 0.88rem; font-weight: 900; color: white;
    line-height: 1.1; white-space: nowrap;
  }

  .rw-island-metric {
    display: flex; align-items: center; gap: 0.25rem;
    padding-left: 0.5rem;
    border-left: 1.5px solid rgba(255,255,255,0.2);
  }
  .rw-island-metric-val {
    font-family: 'Nunito', sans-serif;
    font-size: 0.95rem; font-weight: 900; color: white;
  }

  .rw-island-chevron {
    width: 22px; height: 22px; border-radius: 50%;
    background: rgba(255,255,255,0.15);
    display: flex; align-items: center; justify-content: center;
    flex-shrink: 0;
    transition: transform 0.3s cubic-bezier(0.34,1.56,0.64,1);
  }
  .rw-island-pill.open .rw-island-chevron { transform: rotate(180deg); }

  /* Shimmer skeleton line */
  .rw-skel-row {
    display:flex;align-items:center;justify-content:space-between;
    background:white;border:2.5px solid #f3f4f6;border-radius:18px;
    padding:0.7rem 1rem;
  }
  .rw-skel-left { display:flex;align-items:center;gap:0.75rem; }
  .rw-skel-circle { border-radius:50%;background:#f3f4f6;animation:rwShimmer 1.5s ease-in-out infinite; }
  .rw-skel-line   { border-radius:6px;background:#f3f4f6;animation:rwShimmer 1.5s ease-in-out infinite; }
  @keyframes rwShimmer {
    0%,100%{opacity:1;} 50%{opacity:0.4;}
  }
`;

const TABS = [
  { id: "xp", label: "XP", icon: <Zap size={13} /> },
  {
    id: "questions",
    label: "Questions",
    icon: <LucideBadgeQuestionMark size={13} />,
  },
  { id: "streak", label: "Streak", icon: <Flame size={13} /> },
] as const;

type TabId = (typeof TABS)[number]["id"];

const trophies = [firstTrophy, secondTrophy, thirdTrophy];

const SkeletonRows = () => (
  <div className="rw-skeleton">
    {Array.from({ length: 7 }).map((_, i) => (
      <div key={i} className="rw-skel-row">
        <div className="rw-skel-left">
          <div className="rw-skel-circle" style={{ width: 36, height: 36 }} />
          <div className="rw-skel-circle" style={{ width: 40, height: 40 }} />
          <div
            className="rw-skel-line"
            style={{ width: 100 + i * 10, height: 12 }}
          />
        </div>
        <div className="rw-skel-line" style={{ width: 40, height: 12 }} />
      </div>
    ))}
  </div>
);

export const Rewards = () => {
  const user = useAuthStore((state) => state.user);
  const [time, setTime] = useState("bottom");
  const [activeTab, setActiveTab] = useState<TabId>("xp");
  const [leaderboard, setLeaderboard] = useState<Leaderboard>();
  const [loading, setLoading] = useState(false);
  const [islandOpen, setIslandOpen] = useState(false);
  const { setUserXp } = useExamConfigStore();

  useEffect(() => {
    const fetch = async () => {
      if (!user) return;
      try {
        setLoading(true);
        const authStorage = localStorage.getItem("auth-storage");
        if (!authStorage) return;
        const {
          state: { token },
        } = JSON.parse(authStorage) as { state?: { token?: string } };
        if (!token) return;
        const response = await api.fetchLeaderboard(token);
        setLeaderboard(response);
        setUserXp(response.user_rank.total_xp);
      } catch (e) {
        console.error(e);
      } finally {
        setLoading(false);
      }
    };
    fetch();
  }, [user]);

  const isTopThree = (leaderboard?.user_rank?.rank ?? Infinity) <= 3;

  const metricValue = (u: (typeof leaderboard.top_users)[0]) =>
    activeTab === "xp" ? u.total_xp : activeTab === "questions" ? "—" : "—";

  const metricIcon = () =>
    activeTab === "xp" ? (
      <Zap size={17} className="rw-xp-chip" />
    ) : activeTab === "questions" ? (
      <LucideBadgeQuestionMark size={17} className="rw-q-chip" />
    ) : (
      <Flame size={17} className="rw-fire-chip" />
    );

  const userMetric =
    activeTab === "xp"
      ? leaderboard?.user_rank.total_xp
      : activeTab === "questions"
        ? "23"
        : "5";

  return (
    <div className="rw-screen">
      <style>{STYLES}</style>

      {/* Blobs */}
      <div className="rw-blob rw-blob-1" />
      <div className="rw-blob rw-blob-2" />
      <div className="rw-blob rw-blob-3" />
      <div className="rw-blob rw-blob-4" />

      {/* Dots */}
      {DOTS.map((d, i) => (
        <div
          key={i}
          className="rw-dot"
          style={
            {
              width: d.size,
              height: d.size,
              background: d.color,
              top: d.top,
              left: d.left,
              right: d.right,
              animationDelay: d.delay,
              animationDuration: `${5 + i * 0.5}s`,
            } as React.CSSProperties
          }
        />
      ))}

      {/* Sticky top: header + controls */}
      <div className="rw-sticky-top">
        <div className="rw-sticky-top-inner">
          {/* Header */}
          <header className="rw-header rw-anim">
            <h1 className="rw-title">🏆 Leaderboard</h1>
            {loading ? (
              <div
                style={{
                  height: 16,
                  width: 180,
                  background: "#f3f4f6",
                  borderRadius: 8,
                  animation: "rwShimmer 1.5s ease-in-out infinite",
                }}
              />
            ) : (
              <p className="rw-rank-text">
                You're <span>#{leaderboard?.user_rank.rank}</span> — keep
                grinding!
              </p>
            )}
          </header>

          {/* Controls */}
          <div className="rw-controls rw-anim rw-anim-1">
            <div className="rw-tabs">
              {TABS.map((tab) => (
                <button
                  key={tab.id}
                  className={`rw-tab-btn${activeTab === tab.id ? " active" : ""}`}
                  onClick={() => setActiveTab(tab.id)}
                >
                  {tab.icon} {tab.label}
                </button>
              ))}
            </div>

            <DropdownMenu>
              <DropdownMenuTrigger asChild>
                <button className="rw-filter-btn">
                  {formatTimeFilter(time)} <ChevronDown size={13} />
                </button>
              </DropdownMenuTrigger>
              <DropdownMenuContent
                style={{ fontFamily: "'Nunito',sans-serif", fontWeight: 700 }}
              >
                <DropdownMenuRadioGroup value={time} onValueChange={setTime}>
                  <DropdownMenuRadioItem value="today">
                    Today
                  </DropdownMenuRadioItem>
                  <DropdownMenuRadioItem value="week">
                    Week
                  </DropdownMenuRadioItem>
                  <DropdownMenuRadioItem value="month">
                    Month
                  </DropdownMenuRadioItem>
                  <DropdownMenuRadioItem value="alltime">
                    All Time
                  </DropdownMenuRadioItem>
                </DropdownMenuRadioGroup>
              </DropdownMenuContent>
            </DropdownMenu>
          </div>
        </div>
        {/* end rw-sticky-top-inner */}
      </div>
      {/* end rw-sticky-top */}

      {/* Scrollable list */}
      <div className="rw-scroll-area">
        <div className="rw-scroll-inner">
          <div
            className="rw-list rw-anim rw-anim-2"
            style={{ paddingTop: "1rem" }}
          >
            {loading ? (
              <SkeletonRows />
            ) : (
              leaderboard?.top_users.map((u, index) => {
                const top = index < 3;
                const rowClass = `rw-row${index === 0 ? " top-1" : index === 1 ? " top-2" : index === 2 ? " top-3" : ""}`;
                return (
                  <div key={u.user_id} className={rowClass}>
                    <div className="rw-row-left">
                      <div className="rw-rank-cell">
                        {top ? (
                          <img
                            src={trophies[index]}
                            alt={`#${index + 1}`}
                            style={{ width: 32, height: 32 }}
                          />
                        ) : (
                          <span className="rw-rank-num">{index + 1}</span>
                        )}
                      </div>
                      <Avatar
                        className={`${getRandomColor()}`}
                        style={{ width: 38, height: 38 }}
                      >
                        <AvatarImage src={u.avatar_url} />
                        <AvatarFallback
                          style={{
                            color: "black",
                            fontWeight: 900,
                            fontSize: "0.9rem",
                          }}
                        >
                          {u.name.slice(0, 1).toUpperCase()}
                        </AvatarFallback>
                      </Avatar>
                      <span className="rw-user-name">{u.name}</span>
                    </div>
                    <div className="rw-row-right">
                      <span className="rw-score">{metricValue(u)}</span>
                      {metricIcon()}
                    </div>
                  </div>
                );
              })
            )}
          </div>
        </div>
        {/* end rw-scroll-inner */}
      </div>
      {/* end rw-scroll-area */}

      {/* ── Floating island pill ── */}
      <div className="rw-island-wrap">
        {/* Expanded info card */}
        <div className={`rw-island-card${islandOpen ? " open" : ""}`}>
          <div className="rw-island-stat rank">
            <span className="rw-island-stat-val">
              #{leaderboard?.user_rank?.rank ?? "—"}
            </span>
            <span className="rw-island-stat-label">Rank</span>
          </div>
          <div className="rw-island-stat xp">
            <span className="rw-island-stat-val">
              {leaderboard?.user_rank?.total_xp ?? "—"}
            </span>
            <span className="rw-island-stat-label">Total XP</span>
          </div>
          <div className="rw-island-stat lvl">
            <span className="rw-island-stat-val">
              {leaderboard?.user_rank?.current_level ?? "—"}
            </span>
            <span className="rw-island-stat-label">Level</span>
          </div>
        </div>

        {/* Pill button */}
        <div
          className={`rw-island-pill${islandOpen ? " open" : ""}`}
          onClick={() => !loading && setIslandOpen((o) => !o)}
        >
          {/* Avatar */}
          <div className="rw-island-avatar">
            {leaderboard?.user_rank?.avatar_url ? (
              <span>
                {leaderboard?.user_rank?.name?.slice(0, 1).toUpperCase() ?? "?"}
              </span>
            ) : (
              <span>
                {leaderboard?.user_rank?.name?.slice(0, 1).toUpperCase() ?? "?"}
              </span>
            )}
          </div>
          <div className="rw-island-info">
            <span className="rw-island-you">You</span>
            <span className="rw-island-name">
              {loading ? "Loading..." : (leaderboard?.user_rank?.name ?? "—")}
            </span>
          </div>

          {/* Name */}

          {/* Live metric */}
          <div className="rw-island-metric">
            <span className="rw-island-metric-val">{userMetric}</span>
            {activeTab === "xp" ? (
              <Zap size={15} color="rgba(255,255,255,0.8)" />
            ) : activeTab === "questions" ? (
              <LucideBadgeQuestionMark
                size={15}
                color="rgba(255,255,255,0.8)"
              />
            ) : (
              <Flame size={15} color="rgba(255,255,255,0.8)" />
            )}
          </div>

          {/* Chevron */}
          <div className="rw-island-chevron">
            <ChevronDown size={13} color="white" />
          </div>
        </div>
      </div>
    </div>
  );
};
