import React, { useMemo, useRef } from "react";
import { ChevronLeft, ChevronRight, Heart } from "lucide-react";

export default function PokerSection() {
  const sliderRef = useRef(null);

   const games = useMemo(
     () => [
       {
         title: "Super Ace",
         image: "/assets/cat/1.png",
         provider: "JILI",
         favorite: false,
       },
       {
         title: "Wild Bounty Showdown",
         image: "/assets/cat/2.png",
         provider: "PG",
         favorite: true,
       },
       {
         title: "Poker Kingdom",
         image: "/assets/cat/3.png",
         provider: "JILI",
         favorite: true,
       },
       {
         title: "Aviator",
         image: "/assets/cat/4.png",
         provider: "",
         favorite: true,
         featured: true,
       },
       {
         title: "FlyX",
         image: "/assets/cat/5.png",
         provider: "",
         favorite: false,
       },
       {
         title: "Fortune Gems",
         image: "/assets/cat/6.png",
         provider: "JILI",
         favorite: false,
       },
       {
         title: "Royal Ace",
         image: "/assets/cat/7.png",
         provider: "YB",
         favorite: false,
       },
       {
         title: "Chicky Run",
         image: "/assets/cat/8.png",
         provider: "PG",
         favorite: false,
       },
       {
         title: "Boxing King",
         image: "/assets/cat/9.png",
         provider: "JILI",
         favorite: false,
       },
       {
         title: "Pinata Wins",
         image: "/assets/cat/10.png",
         provider: "PG",
         favorite: true,
       },
       {
         title: "Super Elements",
         image: "/assets/cat/11.png",
         provider: "FC",
         favorite: true,
       },
       {
         title: "Magic Ace",
         image: "/assets/cat/11.png",
         provider: "JDB",
         favorite: true,
       },
     ],
     []
   );
  const columns = Math.ceil(games.length / 2);

  const scrollCards = (dir) => {
    if (!sliderRef.current) return;
    sliderRef.current.scrollBy({
      left: dir === "next" ? 330 : -330,
      behavior: "smooth",
    });
  };

  return (
    <>
      <section className="poker-section">
        <div className="section-head">
          <h2>POKER</h2>

          <div className="head-actions">
            <button className="see-all-btn">See All</button>

            <button
              className="nav-btn"
              onClick={() => scrollCards("prev")}
              aria-label="Previous poker games"
            >
              <ChevronLeft size={20} />
            </button>

            <button
              className="nav-btn"
              onClick={() => scrollCards("next")}
              aria-label="Next poker games"
            >
              <ChevronRight size={20} />
            </button>
          </div>
        </div>

        <div className="slider" ref={sliderRef}>
          <div className="games-track">
            {games.map((game, index) => {
              const rowClass = index >= columns ? "row-second" : "row-first";

              return (
                <article key={index} className={`game-card ${rowClass}`}>
                  <img src={game.image} alt={game.title} className="game-image" />

                  <div className="card-overlay" />
                  <div className="bottom-fade" />
                  <span className="card-sweep" />

                  {game.favorite && (
                    <button
                      className="fav-btn"
                      aria-label={`Favorite ${game.title}`}
                    >
                      <Heart size={15} fill="currentColor" />
                    </button>
                  )}

                  {game.cornerBadge && (
                    <div className="corner-badge">{game.cornerBadge}</div>
                  )}

                  {game.badge && <div className="provider-badge">{game.badge}</div>}

                  <div className="card-content">
                    {game.providerTop ? (
                      <div className="provider-top">{game.providerTop}</div>
                    ) : (
                      <div className="provider-top empty" />
                    )}

                    <div className="provider-bottom">{game.providerBottom}</div>
                  </div>
                </article>
              );
            })}
          </div>
        </div>
      </section>

      <style jsx>{`
        .poker-section {
          width: 100%;
          padding: 14px 14px 12px;
          border-radius: 18px;
         
          position: relative;
        }

        .poker-section::before {
          content: "";
          position: absolute;
          inset: 0;
        
          pointer-events: none;
        }

        .section-head {
          display: flex;
          align-items: center;
          justify-content: space-between;
          gap: 10px;
          margin-bottom: 14px;
          position: relative;
          z-index: 2;
        }

        .section-head h2 {
          margin: 0;
          font-size: 18px;
          line-height: 1;
          font-weight: 900;
          letter-spacing: 0.8px;
          color: #20ffd2;
          text-transform: uppercase;
        }

        .head-actions {
          display: flex;
          align-items: center;
          gap: 10px;
          flex-shrink: 0;
        }

        .see-all-btn {
          height: 40px;
          padding: 0 14px;
          border: 1px solid rgba(7, 40, 47, 0.9);
          border-radius: 12px;
          background: linear-gradient(180deg, #0f7077 0%, #0a5b61 100%);
          color: #ffcb2f;
          font-size: 14px;
          font-weight: 800;
          box-shadow:
            inset 0 1px 0 rgba(100, 255, 241, 0.08),
            0 3px 0 rgba(0, 0, 0, 0.2);
          cursor: pointer;
        }

        .nav-btn {
          width: 40px;
          height: 40px;
          border: 1px solid rgba(7, 40, 47, 0.9);
          border-radius: 12px;
          background: linear-gradient(180deg, #0d6d74 0%, #0a5860 100%);
          color: #dffffc;
          display: inline-flex;
          align-items: center;
          justify-content: center;
          cursor: pointer;
          box-shadow:
            inset 0 1px 0 rgba(100, 255, 241, 0.08),
            0 3px 0 rgba(0, 0, 0, 0.2);
        }

        .slider {
          overflow-x: auto;
          overflow-y: hidden;
          scrollbar-width: none;
          -ms-overflow-style: none;
          scroll-behavior: smooth;
          position: relative;
          z-index: 2;
        }

        .slider::-webkit-scrollbar {
          display: none;
        }

        .games-track {
          display: grid;
          grid-auto-flow: column;
          grid-template-rows: repeat(2, 1fr);
          grid-auto-columns: 145px;
          gap: 4px;
          min-width: max-content;
          padding-right: 8px;
        }

        .game-card {
          position: relative;
          width: 145px;
          height: 200px;
          border-radius: 16px;
          overflow: hidden;
          background: linear-gradient(180deg, #0d6a6d 0%, #0a4d52 100%);
          border: 1px solid rgba(30, 145, 144, 0.58);
          box-shadow:
            inset 0 1px 0 rgba(255, 255, 255, 0.05),
            0 8px 18px rgba(0, 0, 0, 0.22);
          flex-shrink: 0;
        }

        .game-image {
          width: 100%;
          height: 100%;
          object-fit: cover;
          display: block;
          transform: scale(1.01);
        }

        .game-card.row-second .game-image {
          opacity: 0.84;
        }

        .card-overlay {
          position: absolute;
          inset: 0;
          background:
            linear-gradient(
              180deg,
              rgba(255, 255, 255, 0.03) 0%,
              rgba(255, 255, 255, 0) 20%,
              rgba(0, 0, 0, 0.05) 52%,
              rgba(0, 0, 0, 0.18) 100%
            );
          pointer-events: none;
          z-index: 1;
        }

        .bottom-fade {
          position: absolute;
          left: 0;
          right: 0;
          bottom: 0;
          height: 48%;
          background: linear-gradient(
            180deg,
            rgba(0, 0, 0, 0) 0%,
            rgba(0, 33, 39, 0.16) 28%,
            rgba(0, 28, 34, 0.52) 64%,
            rgba(0, 24, 30, 0.88) 100%
          );
          pointer-events: none;
          z-index: 2;
        }

        .game-card.row-second .bottom-fade {
          background: linear-gradient(
            180deg,
            rgba(0, 0, 0, 0.04) 0%,
            rgba(0, 24, 30, 0.26) 30%,
            rgba(0, 20, 25, 0.64) 65%,
            rgba(0, 16, 22, 0.96) 100%
          );
        }

        .card-sweep {
          position: absolute;
          inset: -70%;
          pointer-events: none;
          z-index: 3;
          background: linear-gradient(
            110deg,
            rgba(255, 255, 255, 0) 22%,
            rgba(255, 255, 255, 0.05) 38%,
            rgba(255, 255, 255, 0.12) 45%,
            rgba(255, 255, 255, 0.24) 50%,
            rgba(255, 255, 255, 0.12) 55%,
            rgba(255, 255, 255, 0.05) 62%,
            rgba(255, 255, 255, 0) 76%
          );
          mix-blend-mode: screen;
          opacity: 1;
          transform: translateX(-95%) rotate(12deg);
          animation: cardSweepMove 3.2s linear infinite;
          will-change: transform;
        }

        .game-card.row-second .card-sweep {
          opacity: 0.68;
        }

        .fav-btn {
          position: absolute;
          top: 8px;
          right: 8px;
          width: 28px;
          height: 28px;
          border: none;
          border-radius: 999px;
          background: rgba(255, 255, 255, 0.96);
          color: #8b8b8b;
          display: flex;
          align-items: center;
          justify-content: center;
          box-shadow: 0 4px 10px rgba(0, 0, 0, 0.16);
          z-index: 6;
          cursor: pointer;
        }

        .corner-badge {
          position: absolute;
          top: 0;
          right: 0;
          width: 52px;
          height: 52px;
          display: flex;
          align-items: flex-start;
          justify-content: center;
          padding-top: 8px;
          background: linear-gradient(180deg, #ffbb73 0%, #f08c37 100%);
          color: #ffffff;
          font-size: 10px;
          font-weight: 900;
          letter-spacing: 0.3px;
          text-transform: uppercase;
          clip-path: polygon(100% 0, 0 0, 100% 100%);
          z-index: 5;
          text-shadow: 0 1px 2px rgba(0, 0, 0, 0.2);
          transform: rotate(0deg);
        }

        .provider-badge {
          position: absolute;
          top: 0;
          right: 0;
          min-width: 42px;
          height: 36px;
          padding: 0 8px;
          border-bottom-left-radius: 12px;
          background: linear-gradient(180deg, #f9d96b 0%, #b87b10 100%);
          color: #fff2c4;
          font-size: 12px;
          font-weight: 900;
          display: flex;
          align-items: center;
          justify-content: center;
          text-shadow: 0 1px 0 rgba(0, 0, 0, 0.18);
          z-index: 5;
        }

        .card-content {
          position: absolute;
          left: 0;
          right: 0;
          bottom: 10px;
          text-align: center;
          padding: 0 8px;
          z-index: 4;
        }

        .provider-top {
          color: rgba(227, 194, 101, 0.95);
          font-size: 11px;
          line-height: 1.05;
          font-weight: 800;
          letter-spacing: 0.5px;
          text-transform: uppercase;
          text-shadow: 0 2px 6px rgba(0, 0, 0, 0.34);
          margin-bottom: 4px;
        }

        .provider-top.empty {
          height: 11px;
          margin-bottom: 4px;
        }

        .provider-bottom {
          color: #ffffff;
          font-size: 11px;
          line-height: 1;
          font-weight: 900;
          letter-spacing: 0.7px;
          text-transform: uppercase;
          text-shadow: 0 2px 6px rgba(0, 0, 0, 0.4);
        }

        @keyframes cardSweepMove {
          0% {
            transform: translateX(-95%) rotate(12deg);
          }
          100% {
            transform: translateX(95%) rotate(12deg);
          }
        }

        @media (max-width: 768px) {
          .poker-section {
            padding: 12px 12px 10px;
            border-radius: 16px;
          }

          .section-head {
            margin-bottom: 12px;
          }

          .section-head h2 {
            font-size: 17px;
          }

          .head-actions {
            gap: 8px;
          }

          .see-all-btn {
            height: 38px;
            padding: 0 13px;
            border-radius: 11px;
            font-size: 13px;
          }

          .nav-btn {
            width: 38px;
            height: 38px;
            border-radius: 11px;
          }

          .games-track {
            grid-auto-columns: 145px;
            gap: 4px;
          }

          .game-card {
            width: 145px;
            height: 198px;
            border-radius: 15px;
          }

          .bottom-fade {
            height: 48%;
          }
        }
      `}</style>
    </>
  );
}