"use client";

import { useTheme } from "next-themes";
import { Moon, Sun } from "lucide-react";
import { Button } from "@/components/ui/button";
import { useEffect, useState } from "react";

export function ThemeToggle() {
  const { theme, setTheme } = useTheme();
  const [mounted, setMounted] = useState(false);

  useEffect(() => {
    setMounted(true);
  }, []);

  if (!mounted) {
    return (
      <Button
        variant="ghost"
        size="icon"
        className="h-9 w-9 rounded-full"
        aria-label="Changer de thème"
      >
        <Sun className="h-5 w-5" />
      </Button>
    );
  }

  return (
    <Button
      variant="ghost"
      size="icon"
      onClick={() => setTheme(theme === "dark" ? "light" : "dark")}
      className="h-10 w-10 rounded-full hover:bg-secondary dark:hover:bg-gray-800"
      aria-label="Changer de thème"
    >
      {theme === "dark" ? (
        <Sun className="h-5 w-5 text-primary dark:text-yellow-400" />
      ) : (
        <Moon className="h-5 w-5 text-primary dark:text-blue-300" />
      )}
    </Button>
  );
}

