import { Button } from "@/components/ui/button";
import { Input } from "@/components/ui/input";
import { Label } from "@/components/ui/label";
import { useAuth } from "@/_core/hooks/useAuth";
import { trpc } from "@/lib/trpc";
import { useEffect, useState } from "react";
import { assetUrl } from "@/lib/asset";
import { Link, useLocation } from "wouter";
import { toast } from "sonner";

export default function Login() {
  const [, navigate] = useLocation();
  const { user, loading } = useAuth();
  const utils = trpc.useUtils();

  const [email, setEmail] = useState("");
  const [password, setPassword] = useState("");

  const loginMutation = trpc.auth.login.useMutation({
    onSuccess: async () => {
      await utils.auth.me.invalidate();
      navigate("/");
    },
    onError: error => {
      toast.error(error.message || "Não foi possível entrar.");
    },
  });

  useEffect(() => {
    if (!loading && user) {
      navigate("/");
    }
  }, [loading, user, navigate]);

  const handleSubmit = (e: React.FormEvent) => {
    e.preventDefault();
    loginMutation.mutate({ email, password });
  };

  return (
    <div className="min-h-screen flex items-center justify-center bg-sof-borgonha px-4">
      <div className="w-full max-w-md">
        <div className="text-center mb-8">
          <img src={assetUrl("/logo_vt.jpg")} alt="SpotOnFide" className="w-20 h-20 rounded-xl object-cover mx-auto mb-4" />
          <h1 className="text-3xl font-bold font-display text-sof-text">SpotOnFide</h1>
          <p className="text-sof-text-3 text-sm mt-1">Entre na sua conta</p>
        </div>

        <form
          onSubmit={handleSubmit}
          className="bg-sof-surface border border-sof-border rounded-2xl p-8 space-y-5"
        >
          <div className="space-y-2">
            <Label htmlFor="email" className="text-sof-text-2">
              E-mail
            </Label>
            <Input
              id="email"
              type="email"
              required
              autoComplete="email"
              value={email}
              onChange={e => setEmail(e.target.value)}
              className="bg-sof-borgonha border-sof-border text-sof-text"
              placeholder="voce@exemplo.com"
            />
          </div>

          <div className="space-y-2">
            <Label htmlFor="password" className="text-sof-text-2">
              Senha
            </Label>
            <Input
              id="password"
              type="password"
              required
              autoComplete="current-password"
              value={password}
              onChange={e => setPassword(e.target.value)}
              className="bg-sof-borgonha border-sof-border text-sof-text"
              placeholder="••••••••"
            />
          </div>

          <Button
            type="submit"
            disabled={loginMutation.isPending}
            className="w-full bg-sof-gold text-sof-borgonha hover:bg-sof-gold-light font-bold h-11"
          >
            {loginMutation.isPending ? "Entrando..." : "Entrar"}
          </Button>

          <p className="text-center text-sm text-sof-text-3">
            Ainda não tem conta?{" "}
            <Link href="/cadastro" className="text-sof-gold hover:underline">
              Cadastre-se
            </Link>
          </p>
        </form>

        <div className="text-center mt-6">
          <Link href="/" className="text-sm text-sof-text-3 hover:text-sof-gold transition-colors">
            ← Voltar para o início
          </Link>
        </div>
      </div>
    </div>
  );
}
