"use client";

import { useState } from "react";
import UserManagement from "@/components/admin/UserManagement";
import EventManagement from "@/components/admin/EventManagement";
import FeedbackManagement from "@/components/admin/FeedbackManagement";
import ChallengeManagement from "@/components/admin/ChallengeManagement";
import BackgroundPreferences from "@/components/admin/BackgroundPreferences";
import EventPointsPreferences from "@/components/admin/EventPointsPreferences";
import EventFeedbackPointsPreferences from "@/components/admin/EventFeedbackPointsPreferences";
import HousePointsPreferences from "@/components/admin/HousePointsPreferences";
import { Button, Card, SectionTitle } from "@/components/ui";

interface SitePreferences {
  id: string;
  homeBackground: string | null;
  eventsBackground: string | null;
  leaderboardBackground: string | null;
  challengesBackground: string | null;
  eventRegistrationPoints: number;
  eventFeedbackPoints: number;
  houseJoinPoints: number;
  houseLeavePoints: number;
  houseCreatePoints: number;
}

interface AdminPanelProps {
  initialPreferences: SitePreferences;
}

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

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-16 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>
          <Button
            onClick={() => setActiveSection("challenges")}
            variant={activeSection === "challenges" ? "primary" : "secondary"}
            size="md"
            className={activeSection === "challenges" ? "bg-pixel-gold/10" : ""}
          >
            Défis
          </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>
            </div>
            <div className="space-y-4">
              <BackgroundPreferences initialPreferences={initialPreferences} />
              <EventPointsPreferences initialPreferences={initialPreferences} />
              <EventFeedbackPointsPreferences initialPreferences={initialPreferences} />
              <HousePointsPreferences 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>
        )}

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