'use client';

import Link from "next/link";
import Button from "@/components/Button";
import GamePeriod from "@/components/GamePeriod";
import GrandPrize from "@/components/GrandPrize";
import AboutContest from "@/components/AboutContest";
import TeaIconsBackground from "@/components/TeaIconsBackground";
import { PrizeCard } from "@/components/ui";
import { useState, useEffect } from "react";
import { useAuth } from "@/contexts/AuthContext";
import { ROUTES } from "@/utils/constants";
import { useRouter } from "next/navigation";

const HOME_PRIZES = [
  { imageSrc: "/images/lots/infuseur.png", imageAlt: "", badge: "60%", title: "Infuseur à thé premium", description: "Un infuseur en acier inoxydable de haute qualité pour ressortir les arômes de vos thés en vrac" },
  { imageSrc: "/images/lots/the-detox.png", imageAlt: "", badge: "20%", title: "Boîte 100g thé détox", description: "Mélange détox aux plantes bio" },
  { imageSrc: "/images/lots/the-signature.png", imageAlt: "", badge: "10%", title: "Boîte 100g thé signature", description: "Notre mélange signature exclusif" },
  { imageSrc: "/images/lots/coffret-39.png", imageAlt: "", badge: "6%", title: "Coffret découverte 39€", description: "Sélection de nos meilleurs thés" },
  { imageSrc: "/images/lots/coffret-69.jpg", imageAlt: "", badge: "4%", title: "Coffret prestige 69€", description: "Une expérience complète" },
  { imageSrc: "/images/lots/grand-prix.png", imageAlt: "", badge: "1 an de THÉ", title: "Tirage Final", description: "Livraison mensuelle pendant 12 mois", isGrandPrix: true },
];

export default function HomePage() {
  const [animationKey] = useState(Date.now());
  const { isAuthenticated, user } = useAuth();
  const router = useRouter();

  // Redirect admin/employee to their dashboard
  useEffect(() => {
    if (isAuthenticated && user) {
      const role = user.role?.toUpperCase();
      if (role === 'ADMIN') {
        router.replace(ROUTES.ADMIN_DASHBOARD);
      } else if (role === 'EMPLOYEE') {
        router.replace(ROUTES.EMPLOYEE_DASHBOARD);
      }
    }
  }, [isAuthenticated, user, router]);

  // Don't show home page for admin/employee
  if (isAuthenticated && user) {
    const role = user.role?.toUpperCase();
    if (role === 'ADMIN' || role === 'EMPLOYEE') {
      return null;
    }
  }

  return (
    <div className="min-h-screen -mt-[4.5rem] relative">
      <TeaIconsBackground animationKey={animationKey} />

      {/* Hero Section - Bannière principale */}
      <section className="relative min-h-screen flex items-center justify-center overflow-hidden pt-20 md:pt-0">

        <div className="w-full relative z-10">
          <div className="w-full text-center px-4">
            <h1 className="text-3xl sm:text-4xl md:text-6xl lg:text-7xl font-bold text-primary-500 mb-4 md:mb-6 leading-tight drop-shadow-sm tracking-wide">
              Jeu Concours - Thé Tip Top
            </h1>
            <p className="text-xl sm:text-2xl md:text-5xl lg:text-6xl font-semibold text-primary-600 mb-4 md:mb-6 leading-snug md:leading-relaxed tracking-wide">
              Célébrons l'ouverture de notre 10<sup>ème</sup> boutique à <span className="text-primary-700 font-bold">Nice</span>
            </p>
            <p className="text-base sm:text-lg md:text-2xl lg:text-3xl text-gray-500 mb-8 md:mb-10 leading-relaxed tracking-wide px-2">
              Participez à notre concours - 100% des participants gagnent un lot !
            </p>

            {/* Bouton principal */}
            <div className="flex flex-col gap-4 justify-center items-center px-4">
              <Link href={isAuthenticated ? ROUTES.GAME : "/register"}>
                <div className="relative inline-block">
                  {/* Anneau qui pulse autour du bouton */}
                  <span className="absolute inset-0 rounded-full border-4 border-primary-500 animate-ping opacity-60"></span>

                  <button className="relative bg-primary-500 hover:bg-primary-600 text-white font-bold text-lg sm:text-xl md:text-2xl px-10 py-4 sm:px-16 sm:py-6 md:px-20 md:py-7 rounded-full shadow-[0_0_25px_rgba(11,96,41,0.6)] hover:shadow-[0_0_40px_rgba(11,96,41,0.8)] transition-all duration-300 hover:scale-105">
                    Jouer maintenant
                  </button>
                </div>
              </Link>

            </div>
          </div>
        </div>
      </section>

      {/* Game Period & Countdown Section */}
      <section className="py-20 bg-white/60 backdrop-blur-sm">
        <div className="container mx-auto px-4">
          <div className="max-w-6xl mx-auto">
            {/* Game Period Cards */}
            <GamePeriod />

            {/* Grand Prize Banner */}
            <GrandPrize
              prizeAmount="360€"
              drawDate={new Date('2026-02-01')}
            />
          </div>
        </div>
      </section>

      {/* About Contest Section */}
      <AboutContest />

      {/* How to Participate Section */}
      <section className="py-20 bg-white/60 backdrop-blur-sm">
        <div className="container mx-auto px-4">
          <div className="max-w-5xl mx-auto">
            <h2 className="text-4xl md:text-5xl font-bold text-center text-primary-600 mb-4">
              Comment participer?
            </h2>
            <p className="text-center text-gray-600 mb-16 text-lg max-w-2xl mx-auto">
              3 étapes simples pour découvrir votre lot et rejoindre nos milliers de gagnants
            </p>

            <div className="grid md:grid-cols-3 gap-12">
              {/* Step 1 */}
              <div className="text-center relative">
                <div className="absolute top-0 right-0 w-8 h-8 bg-primary-500 text-white rounded-full flex items-center justify-center font-bold text-sm">
                  1
                </div>
                <div className="bg-gradient-to-br from-primary-100 to-primary-200 w-20 h-20 rounded-full flex items-center justify-center mx-auto mb-6 shadow-lg">
                  <svg className="w-10 h-10 text-primary-600" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                    <path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M16 7a4 4 0 11-8 0 4 4 0 018 0zM12 14a7 7 0 00-7 7h14a7 7 0 00-7-7z" />
                  </svg>
                </div>
                <h3 className="text-2xl font-bold mb-3 text-gray-900">Inscrivez-vous</h3>
                <p className="text-gray-600 leading-relaxed">
                  Créez votre compte rapidement avec Google, Facebook ou votre email
                </p>
              </div>

              {/* Step 2 */}
              <div className="text-center relative">
                <div className="absolute top-0 right-0 w-8 h-8 bg-primary-500 text-white rounded-full flex items-center justify-center font-bold text-sm">
                  2
                </div>
                <div className="bg-gradient-to-br from-primary-100 to-primary-200 w-20 h-20 rounded-full flex items-center justify-center mx-auto mb-6 shadow-lg">
                  <svg className="w-10 h-10 text-primary-600" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                    <path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M15 5v2m0 4v2m0 4v2M5 5a2 2 0 00-2 2v3a2 2 0 110 4v3a2 2 0 002 2h14a2 2 0 002-2v-3a2 2 0 110-4V7a2 2 0 00-2-2H5z" />
                  </svg>
                </div>
                <h3 className="text-2xl font-bold mb-3 text-gray-900">Entrez votre code</h3>
                <p className="text-gray-600 leading-relaxed">
                  Saisissez le code à 10 caractères de votre ticket
                </p>
              </div>

              {/* Step 3 */}
              <div className="text-center relative">
                <div className="absolute top-0 right-0 w-8 h-8 bg-primary-500 text-white rounded-full flex items-center justify-center font-bold text-sm">
                  3
                </div>
                <div className="bg-gradient-to-br from-primary-100 to-primary-200 w-20 h-20 rounded-full flex items-center justify-center mx-auto mb-6 shadow-lg">
                  <svg className="w-10 h-10 text-primary-600" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                    <path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M12 8v13m0-13V6a2 2 0 112 2h-2zm0 0V5.5A2.5 2.5 0 109.5 8H12zm-7 4h14M5 12a2 2 0 110-4h14a2 2 0 110 4M5 12v7a2 2 0 002 2h10a2 2 0 002-2v-7" />
                  </svg>
                </div>
                <h3 className="text-2xl font-bold mb-3 text-gray-900">Découvrez votre lot</h3>
                <p className="text-gray-600 leading-relaxed">
                  Résultat immédiat ! 100% des participants remportent un prix
                </p>
              </div>
            </div>
          </div>
        </div>
      </section>

      {/* Prizes Section */}
      <section className="py-20 bg-transparent">
        <div className="container mx-auto px-4">
          <div className="max-w-6xl mx-auto">
            <h2 className="text-4xl md:text-5xl font-bold text-center text-primary-600 mb-4">
              Lots à gagner
            </h2>
            <p className="text-center text-gray-600 mb-16 text-lg max-w-3xl mx-auto">
              Découvrez nos magnifiques prix, des accessoires premium aux coffrets exclusifs
            </p>

            <div className="grid md:grid-cols-2 lg:grid-cols-3 gap-6">
              {HOME_PRIZES.map((prize, index) => (
                <PrizeCard key={index} {...prize} />
              ))}
            </div>

            <div className="text-center mt-12">
              <Link href="/lots">
                <Button
                  variant="outline"
                  size="lg"
                  className="border-2 border-primary-500 text-primary-500 hover:bg-primary-500 hover:text-white font-semibold px-8"
                >
                  Voir tous les lots
                  <svg className="w-5 h-5 ml-2" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                    <path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M9 5l7 7-7 7" />
                  </svg>
                </Button>
              </Link>
            </div>
          </div>
        </div>
      </section>

    </div>
  );
}
