"use client";

import { useTheme } from "next-themes";
import { useEffect, useState } from "react";
import { Sun, Moon, Monitor } from "lucide-react";

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

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

  if (!mounted) {
    return (
      <button className="p-2 rounded-lg bg-muted" aria-label="Toggle theme">
        <Monitor size={18} />
      </button>
    );
  }

  return (
    <button
      onClick={() => setTheme(theme === "dark" ? "light" : "dark")}
      className="relative p-2.5 rounded-xl bg-muted/50 hover:bg-muted border border-border/50 transition-all duration-300 hover:scale-105 group"
      aria-label="Toggle theme"
    >
      {theme === "dark" ? (
        <Sun
          size={18}
          className="text-amber-400 transition-transform duration-500 group-hover:rotate-90"
        />
      ) : (
        <Moon
          size={18}
          className="text-indigo-500 transition-transform duration-500 group-hover:-rotate-12"
        />
      )}
    </button>
  );
}
