"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="sm"
        className="h-9 w-9 rounded-full p-0"
        aria-label="Changer de thème"
      >
        <Sun className="h-5 w-5" />
      </Button>
    );
  }

  return (
    <Button
      variant="ghost"
      size="sm"
      onClick={() => setTheme(theme === "dark" ? "light" : "dark")}
      className="h-10 w-10 rounded-full hover:bg-secondary dark:hover:bg-gray-800 p-0"
      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>
  );
}

