"use client";

import { useState } from "react";
import Link from "next/link";
import UserManagement from "@/components/UserManagement";
import EventManagement from "@/components/EventManagement";
import FeedbackManagement from "@/components/FeedbackManagement";
import BackgroundPreferences from "@/components/BackgroundPreferences";
import { Button, Card, SectionTitle } from "@/components/ui";

interface SitePreferences {
  id: string;
  homeBackground: string | null;
  eventsBackground: string | null;
  leaderboardBackground: string | null;
}

interface AdminPanelProps {
  initialPreferences: SitePreferences;
}

type AdminSection = "preferences" | "users" | "events" | "feedbacks";

export default function AdminPanel({ initialPreferences }: AdminPanelProps) {
  const [activeSection, setActiveSection] =
    useState<AdminSection>("preferences");

  return (
    <section className="relative w-full min-h-screen flex flex-col items-center overflow-hidden pt-24 pb-16">
      <div className="relative z-10 w-full max-w-6xl mx-auto px-8 py-16">
        <SectionTitle variant="gradient" size="md" className="mb-8 text-center">
          ADMIN
        </SectionTitle>

        {/* Navigation Tabs */}
        <div className="flex gap-4 mb-8 justify-center flex-wrap">
          <Button
            onClick={() => setActiveSection("preferences")}
            variant={activeSection === "preferences" ? "primary" : "secondary"}
            size="md"
            className={
              activeSection === "preferences" ? "bg-pixel-gold/10" : ""
            }
          >
            Préférences UI
          </Button>
          <Button
            onClick={() => setActiveSection("users")}
            variant={activeSection === "users" ? "primary" : "secondary"}
            size="md"
            className={activeSection === "users" ? "bg-pixel-gold/10" : ""}
          >
            Utilisateurs
          </Button>
          <Button
            onClick={() => setActiveSection("events")}
            variant={activeSection === "events" ? "primary" : "secondary"}
            size="md"
            className={activeSection === "events" ? "bg-pixel-gold/10" : ""}
          >
            Événements
          </Button>
          <Button
            onClick={() => setActiveSection("feedbacks")}
            variant={activeSection === "feedbacks" ? "primary" : "secondary"}
            size="md"
            className={activeSection === "feedbacks" ? "bg-pixel-gold/10" : ""}
          >
            Feedbacks
          </Button>
        </div>

        {activeSection === "preferences" && (
          <Card variant="dark" className="p-4 sm:p-6">
            <div className="flex flex-col sm:flex-row sm:items-center sm:justify-between gap-4 mb-6">
              <h2 className="text-xl sm:text-2xl font-gaming font-bold text-pixel-gold break-words">
                Préférences UI Globales
              </h2>
              <Link href="/admin/style-guide" target="_blank">
                <Button variant="primary" size="sm">
                  📖 Voir le Style Guide
                </Button>
              </Link>
            </div>
            <div className="space-y-4">
              <BackgroundPreferences initialPreferences={initialPreferences} />
            </div>
          </Card>
        )}

        {activeSection === "users" && (
          <Card variant="dark" className="p-6">
            <h2 className="text-2xl font-gaming font-bold mb-6 text-pixel-gold">
              Gestion des Utilisateurs
            </h2>
            <UserManagement />
          </Card>
        )}

        {activeSection === "events" && (
          <Card variant="dark" className="p-6">
            <h2 className="text-2xl font-gaming font-bold mb-6 text-pixel-gold">
              Gestion des Événements
            </h2>
            <EventManagement />
          </Card>
        )}

        {activeSection === "feedbacks" && (
          <Card variant="dark" className="p-6">
            <h2 className="text-2xl font-gaming font-bold mb-6 text-pixel-gold">
              Gestion des Feedbacks
            </h2>
            <FeedbackManagement />
          </Card>
        )}
      </div>
    </section>
  );
}
