<?php include 'partials/header.php'; ?>

<?php
$regularCount = 0;
$specialCount = 0;
$automaticCount = 0;
if (!empty($matches)) {
    foreach ($matches as $m) {
        if (($m['category'] ?? '') === 'REGULAR' && ($m['status'] ?? '') === 'ACTIVE') $regularCount++;
        if (($m['category'] ?? '') === 'SPECIAL' && ($m['status'] ?? '') === 'ACTIVE') $specialCount++;
        if (($m['category'] ?? '') === 'AUTOMATIC' && ($m['status'] ?? '') === 'ACTIVE') $automaticCount++;
    }
}
$activePlayers = 1000 + rand(0, 499);
$showTutorial = isset($_GET['tutorial']) && $_GET['tutorial'] === '1';
?>

<div class="space-y-6 pb-28 pt-4" id="home-tab">
  <div class="flex items-center justify-between px-1">
    <div>
      <h2 class="text-[#facc15] font-extrabold text-xl tracking-tight">
        Hi, <?= htmlspecialchars($user['name'] ?? 'Player') ?> 👋
      </h2>
      <p class="text-[#94a3b8] text-xs font-semibold uppercase tracking-wider">
        UID: #<?= htmlspecialchars($user['uid'] ?? '5281') ?>
      </p>
    </div>
    <div class="bg-slate-800/100 border border-slate-700 rounded-lg px-3 py-1.5 text-right">
      <span class="text-[10px] text-slate-400 font-bold block uppercase leading-none">
        AVAILABLE BAL
      </span>
      <span class="text-white font-black text-base">
        ৳ <?= number_format($user['balance'] ?? 0) ?>
      </span>
    </div>
  </div>

  <div
    class="relative overflow-hidden rounded-2xl bg-gradient-to-br from-indigo-900 via-fuchsia-900 to-purple-800 border border-purple-500/30 p-5 shadow-2xl cursor-pointer hover:shadow-purple-500/10 hover:border-purple-500/50 transition-all active:scale-[0.99]"
    onclick="document.getElementById('tutorial-modal').classList.remove('hidden')"
    id="home-promo-banner"
  >
    <div class="relative z-10 max-w-[65%] space-y-3">
      <h3 class="text-white font-black text-2xl leading-tight font-sans tracking-tight">
        কিভাবে <br />
        <span class="text-yellow-300 underline decoration-yellow-400 decoration-wavy underline-offset-4">খেলবেন?</span>
      </h3>
      <p class="text-white/90 text-sm font-medium flex items-center gap-1.5">
        <span class="w-8 h-8 rounded-full bg-red-600 flex items-center justify-center animate-pulse shrink-0 shadow-lg">
          <svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 24 24" fill="currentColor" stroke="currentColor" stroke-width="2" class="translate-x-0.5"><polygon points="5 3 19 12 5 21 5 3"/></svg>
        </span>
        <span class="font-bold underline">ভিডিওটি দেখুন 👉</span>
      </p>
    </div>

    <div class="absolute right-2 bottom-0 top-0 w-[45%] flex items-center justify-center">
      <div class="relative w-full h-[120%] flex items-center justify-end">
        <div class="w-32 h-32 rounded-lg bg-slate-950/40 rotate-[20deg] border-2 border-white/20 p-2 opacity-80 shadow-lg relative translate-x-4">
          <div class="grid grid-cols-2 gap-1 h-full w-full">
            <div class="bg-red-500 rounded-sm"></div>
            <div class="bg-green-500 rounded-sm"></div>
            <div class="bg-blue-500 rounded-sm"></div>
            <div class="bg-yellow-500 rounded-sm"></div>
          </div>
        </div>
        <div class="absolute top-4 left-6 w-12 h-12 bg-white rounded-xl shadow-xl rotate-[35deg] border border-slate-100 flex flex-col justify-between p-2">
          <div class="flex justify-between">
            <div class="w-2.5 h-2.5 bg-slate-900 rounded-full"></div>
            <div class="w-2.5 h-2.5 bg-slate-900 rounded-full"></div>
          </div>
          <div class="flex justify-center">
            <div class="w-2.5 h-2.5 bg-slate-900 rounded-full"></div>
          </div>
          <div class="flex justify-between">
            <div class="w-2.5 h-2.5 bg-slate-900 rounded-full"></div>
            <div class="w-2.5 h-2.5 bg-slate-900 rounded-full"></div>
          </div>
        </div>
        <div class="absolute bottom-6 left-12 w-9 h-9 bg-yellow-400 rounded-lg shadow-lg -rotate-[15deg] border border-amber-300 flex items-center justify-center p-1">
          <div class="grid grid-cols-2 gap-1.5">
            <div class="w-2 h-2 bg-slate-950 rounded-full"></div>
            <div class="w-2 h-2 bg-slate-950 rounded-full"></div>
            <div class="w-2 h-2 bg-slate-950 rounded-full"></div>
            <div class="w-2 h-2 bg-slate-950 rounded-full"></div>
          </div>
        </div>
      </div>
    </div>
  </div>

  <div class="space-y-4">
    <div class="flex items-center justify-between border-b border-slate-800/80 pb-2">
      <h3 class="text-white text-xs font-black tracking-widest uppercase font-mono">
        GAMES & MODES
      </h3>
      <span class="text-[10px] text-green-400 bg-green-500/10 px-2.5 py-0.5 rounded-full font-bold animate-pulse">
        ● <?= $activePlayers ?> Active Players
      </span>
    </div>

    <a
      href="?view=matches&category=REGULAR"
      class="relative group overflow-hidden bg-slate-900/95 border border-slate-800 hover:border-[#facc15]/40 rounded-2xl p-5 cursor-pointer shadow-md hover:shadow-[#facc15]/5 transition-all duration-300 active:scale-[0.98] block"
      id="home-matches-regular"
    >
      <div class="absolute top-4 right-4 bg-yellow-400 text-slate-900 text-xs font-black px-2.5 py-0.5 rounded-full z-10 shadow-lg">
        111
      </div>

      <div class="flex items-center gap-4">
        <div class="w-16 h-16 rounded-xl bg-[#1e293b]/50 border border-slate-700/50 flex items-center justify-center p-2.5 shrink-0 shadow-inner group-hover:scale-105 transition-transform">
          <div class="grid grid-cols-2 gap-1 w-full h-full">
            <div class="bg-red-500 rounded border border-white/20"></div>
            <div class="bg-green-500 rounded border border-white/20"></div>
            <div class="bg-blue-500 rounded border border-white/20"></div>
            <div class="bg-yellow-500 rounded border border-white/20"></div>
          </div>
        </div>

        <div>
          <h4 class="text-slate-100 font-extrabold text-base tracking-tight group-hover:text-yellow-400 transition-colors">
            Ludo Matches
          </h4>
          <p class="text-slate-400 text-xs uppercase font-bold tracking-widest mt-0.5 font-mono">
            REGULAR 1 VS 1
          </p>
          <div class="flex items-center gap-3 mt-2">
            <span class="text-[10px] text-yellow-400 bg-yellow-400/10 px-2 py-0.5 rounded font-bold font-mono">
              <?= $specialCount + $regularCount ?> LIVE Matches
            </span>
            <span class="text-[10px] text-slate-400 font-bold">
              Entry from: ৳ 50
            </span>
          </div>
        </div>
      </div>

      <div class="absolute -right-6 -bottom-6 w-24 h-24 bg-[#1e3a8a]/10 rounded-full blur-xl group-hover:bg-yellow-500/10 transition-colors"></div>
    </a>

    <a
      href="?view=matches&category=AUTOMATIC"
      class="relative group overflow-hidden bg-slate-900/95 border border-slate-800 hover:border-[#facc15]/40 rounded-2xl p-5 cursor-pointer shadow-md hover:shadow-[#facc15]/5 transition-all duration-300 active:scale-[0.98] block"
      id="home-matches-automatic"
    >
      <div class="flex items-center gap-4">
        <div class="w-16 h-16 rounded-xl bg-[#1e293b]/50 border border-slate-700/50 flex items-center justify-center p-2.5 shrink-0 shadow-inner group-hover:scale-105 transition-transform relative">
          <div class="absolute inset-0 bg-gradient-to-tr from-purple-500/20 to-transparent rounded-xl"></div>
          <div class="relative w-full h-full flex items-center justify-center">
            <svg xmlns="http://www.w3.org/2000/svg" width="32" height="32" viewBox="0 0 24 24" fill="none" stroke="#818cf8" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round"><path d="M12 2L2 7l10 5 10-5-10-5Z"/><path d="M2 17l10 5 10-5"/><path d="M2 12l10 5 10-5"/></svg>
          </div>
        </div>

        <div>
          <h4 class="text-slate-100 font-extrabold text-base tracking-tight group-hover:text-yellow-400 transition-colors">
            Ludo Matches
          </h4>
          <p class="text-slate-400 text-xs uppercase font-bold tracking-widest mt-0.5 font-mono">
            AUTOMATIC MATCHMAKER
          </p>
          <div class="flex items-center gap-3 mt-2">
            <span class="text-[10px] text-indigo-400 bg-indigo-400/10 px-2 py-0.5 rounded font-bold font-mono">
              <?= $automaticCount ?: 1 ?> AUTO Matches
            </span>
            <span class="text-[10px] text-slate-400 font-bold">
              Entry from: ৳ 500
            </span>
          </div>
        </div>
      </div>

      <div class="absolute -right-6 -bottom-6 w-24 h-24 bg-purple-500/10 rounded-full blur-xl"></div>
    </a>
  </div>

  <div class="grid grid-cols-2 gap-3 pt-2">
    <a
      href="?view=leaderboard"
      class="bg-[#131b2e] border border-slate-800 p-4 rounded-xl cursor-pointer hover:bg-slate-800/50 active:scale-95 transition-all block"
    >
      <div class="w-8 h-8 rounded-lg bg-yellow-500/10 flex items-center justify-center mb-2">
        <svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="#eab308" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><circle cx="12" cy="8" r="6"/><path d="M15.477 12.89 17 22l-5-3-5 3 1.523-9.11"/></svg>
      </div>
      <span class="text-xs text-slate-400 font-medium block">Leaderboard</span>
      <span class="text-white text-sm font-black mt-1 block">রবিন ১ লাখ+ আয় 🏆</span>
    </a>

    <a
      href="?view=refer-earn"
      class="bg-[#131b2e] border border-slate-800 p-4 rounded-xl cursor-pointer hover:bg-slate-800/50 active:scale-95 transition-all block"
    >
      <div class="w-8 h-8 rounded-lg bg-emerald-500/10 flex items-center justify-center mb-2">
        <svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="#10b981" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M16 21v-2a4 4 0 0 0-4-4H6a4 4 0 0 0-4 4v2"/><circle cx="9" cy="7" r="4"/><path d="M22 21v-2a4 4 0 0 0-3-3.87"/><path d="M16 3.13a4 4 0 0 1 0 7.75"/></svg>
      </div>
      <span class="text-xs text-slate-400 font-medium block">Refer & Earn</span>
      <span class="text-white text-sm font-black mt-1 block">১% লাইফটাইম কমিশন 💸</span>
    </a>
  </div>
</div>

<div id="tutorial-modal" class="fixed inset-0 bg-slate-950/90 z-50 flex items-center justify-center p-4 <?= $showTutorial ? '' : 'hidden' ?>">
  <div class="bg-[#131b2e] w-full max-w-lg rounded-2xl border border-slate-800 p-6 space-y-4 animate-slideDown">
    <div class="flex items-center justify-between">
      <h4 class="text-yellow-400 font-bold text-lg">
        কিভাবে Ludo Master খেলবেন?
      </h4>
      <button
        class="p-1 rounded-full bg-slate-800 text-slate-400 hover:text-slate-100"
        onclick="document.getElementById('tutorial-modal').classList.add('hidden')"
      >
        <svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M18 6 6 18"/><path d="m6 6 12 12"/></svg>
      </button>
    </div>
    <div class="aspect-video bg-neutral-950 rounded-xl relative overflow-hidden border border-slate-800 flex flex-col items-center justify-center p-4 text-center">
      <svg xmlns="http://www.w3.org/2000/svg" width="64" height="64" viewBox="0 0 24 24" fill="none" stroke="#ef4444" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="animate-pulse mb-3"><circle cx="12" cy="12" r="10"/><polygon points="10 8 16 12 10 16 10 8" fill="#ef4444"/></svg>
      <p class="text-white text-sm font-bold">Ludo Master Tutorial - Bengali Video</p>
      <p class="text-slate-500 text-[10px] max-w-xs mt-1">
        (এই ডেমো ভিডিওটি আপনাকে বোঝায় কিভাবে টাকা ডিপোজিট করবেন, ম্যাচ জয়েন করবেন এবং উইনিং ব্যালেন্স তুলবেন)
      </p>
      <button
        class="mt-4 bg-red-600 hover:bg-red-700 text-white font-bold text-xs py-2 px-5 rounded-full transition-all"
        onclick="alert('১. প্রথমে ওয়ালেট পেজে যান এবং \'+ DEPOSIT\' বাটনে ক্লিক করে টাকা এড করুন।\n২. \'Ludo Matches\' কার্ট সিলেক্ট করুন এবং আপনার পছন্দের ম্যাচে \'JOIN\' করুন।\n৩. ম্যাচ শুরুর ২ মিনিটে রুম আইডি দেওয়া হবে।\n৪. রুম কোড দিয়ে Ludo King গেমে যোগ দিন।\n৫. জিতলে স্ক্রিনশট উইন প্রমাণের জন্য \'Upload Evidence\' করুন।'); document.getElementById('tutorial-modal').classList.add('hidden');"
      >
        টিউটোরিয়াল শেষ করুন
      </button>
    </div>
    <div class="text-slate-300 text-xs leading-relaxed space-y-1">
      <p>• ম্যাচ শুরু হওয়ার ২ মিনিট পূর্বে আমাদের অ্যাপের ম্যাচ পেজে "Room Code" দেওয়া হবে।</p>
      <p>• সেটি কপি করে Ludo King অ্যাপের "Play With Friends" -> "Join"-এ পেস্ট করুন।</p>
      <p>• খেলা শেষ হলে বিজয় স্ক্রিনশট প্রমানসহ অবশ্যই আপলোড করবেন ফাস্ট প্রাইজ পাওয়ার জন্য!</p>
    </div>
  </div>
</div>

<?php include 'partials/footer.php'; ?>
