import React, { useState } from "react";
import { Mail, Lock, Stethoscope } from "lucide-react";
import toast from "react-hot-toast";
import { useNavigate } from "react-router-dom";
import { useAuth } from "../hooks/useAuth";
import { authService, userService } from "../services";

const LoginMedico: React.FC = () => {
  const [formData, setFormData] = useState({
    email: "",
    senha: "",
  });
  const [loading, setLoading] = useState(false);

  const navigate = useNavigate();
  const { loginComEmailSenha } = useAuth();

  const handleLogin = async (e: React.FormEvent) => {
    e.preventDefault();
    setLoading(true);

    try {
      console.log("[LoginMedico] Fazendo login com email:", formData.email);

      // Fazer login via API Supabase
      const loginResponse = await authService.login({
        email: formData.email,
        password: formData.senha,
      });

      console.log("[LoginMedico] Login bem-sucedido!", loginResponse);

      // Buscar informações completas do usuário (profile + roles)
      const userInfo = await userService.getUserInfo();
      console.log("[LoginMedico] UserInfo obtido:", userInfo);

      const userName =
        userInfo.profile?.full_name ||
        loginResponse.user.email?.split("@")[0] ||
        "Médico";
      const roles = userInfo.roles || [];

      // Validar se tem permissão (admin, gestor ou medico)
      const isAdmin = roles.includes("admin");
      const isGestor = roles.includes("gestor");
      const isMedico = roles.includes("medico");

      if (!isAdmin && !isGestor && !isMedico) {
        toast.error("Você não tem permissão para acessar esta área");
        await authService.logout();
        setLoading(false);
        return;
      }

      // Fazer login no contexto
      const ok = await loginComEmailSenha(formData.email, formData.senha);

      if (ok) {
        console.log(
          "[LoginMedico] Login bem-sucedido! Navegando para /painel-medico"
        );
        toast.success(`Bem-vindo, ${userName}!`);
        navigate("/painel-medico");
      } else {
        console.error("[LoginMedico] loginComEmailSenha retornou false");
        toast.error("Erro ao processar login");
      }
    } catch (error: unknown) {
      console.error("[LoginMedico] Erro no login:", error);
      const err = error as {
        response?: { data?: { error_description?: string; message?: string } };
        message?: string;
      };
      const errorMessage =
        err?.response?.data?.error_description ||
        err?.response?.data?.message ||
        err?.message ||
        "Erro ao fazer login. Verifique suas credenciais.";
      toast.error(errorMessage);
    } finally {
      setLoading(false);
    }
  };

  return (
    <div className="min-h-screen bg-gradient-to-br from-indigo-50 to-white dark:from-gray-900 dark:to-gray-950 flex items-center justify-center p-4 transition-colors">
      <div className="max-w-md w-full">
        <div className="text-center mb-8">
          <div className="bg-gradient-to-r from-indigo-600 to-indigo-400 dark:from-indigo-700 dark:to-indigo-500 w-16 h-16 rounded-full flex items-center justify-center mx-auto mb-4 shadow-md">
            <Stethoscope className="w-8 h-8 text-white" />
          </div>
          <h1 className="text-3xl font-bold text-gray-900 dark:text-gray-100 mb-2">
            Área do Médico
          </h1>
          <p className="text-gray-600 dark:text-gray-400">
            Faça login para acessar seu painel médico
          </p>
        </div>

        <div
          className="bg-white dark:bg-gray-800 rounded-lg shadow-lg p-8 border border-transparent dark:border-gray-700 transition-colors"
          aria-live="polite"
        >
          <form onSubmit={handleLogin} className="space-y-6" noValidate>
            <div>
              <label
                htmlFor="med_email"
                className="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-2"
              >
                Email
              </label>
              <div className="relative">
                <Mail className="absolute left-3 top-1/2 transform -translate-y-1/2 w-5 h-5 text-gray-400" />
                <input
                  id="med_email"
                  type="email"
                  value={formData.email}
                  onChange={(e) =>
                    setFormData((prev) => ({ ...prev, email: e.target.value }))
                  }
                  className="form-input pl-10 dark:bg-gray-700 dark:border-gray-600 dark:placeholder-gray-400 dark:text-gray-100"
                  placeholder="seu@email.com"
                  required
                  autoComplete="email"
                />
              </div>
            </div>

            <div>
              <label
                htmlFor="med_password"
                className="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-2"
              >
                Senha
              </label>
              <div className="relative">
                <Lock className="absolute left-3 top-1/2 transform -translate-y-1/2 w-5 h-5 text-gray-400" />
                <input
                  id="med_password"
                  type="password"
                  value={formData.senha}
                  onChange={(e) =>
                    setFormData((prev) => ({ ...prev, senha: e.target.value }))
                  }
                  className="form-input pl-10 dark:bg-gray-700 dark:border-gray-600 dark:placeholder-gray-400 dark:text-gray-100"
                  placeholder="Sua senha"
                  required
                  autoComplete="current-password"
                />
              </div>
              <div className="text-right mt-2">
                <button
                  type="button"
                  onClick={async () => {
                    if (!formData.email.trim()) {
                      toast.error("Digite seu email primeiro");
                      return;
                    }
                    try {
                      await authService.requestPasswordReset(formData.email);
                      toast.success(
                        "Email de recuperação enviado! Verifique sua caixa de entrada."
                      );
                    } catch {
                      toast.error("Erro ao enviar email de recuperação");
                    }
                  }}
                  className="text-sm text-indigo-600 dark:text-indigo-400 hover:underline transition-colors"
                >
                  Esqueceu a senha?
                </button>
              </div>
            </div>

            <button
              type="submit"
              disabled={loading}
              className="w-full bg-gradient-to-r from-indigo-600 to-indigo-400 text-white py-3 px-4 rounded-lg font-medium hover:from-indigo-700 hover:to-indigo-500 hover:scale-105 active:scale-95 disabled:opacity-50 disabled:cursor-not-allowed disabled:hover:scale-100 transition-all duration-200 focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-indigo-500 focus-visible:ring-offset-2"
            >
              {loading ? "Entrando..." : "Entrar"}
            </button>

            {/* Divisor OU */}
            <div className="relative my-6">
              <div className="absolute inset-0 flex items-center">
                <div className="w-full border-t border-gray-300 dark:border-gray-600"></div>
              </div>
              <div className="relative flex justify-center text-sm">
                <span className="px-2 bg-white dark:bg-gray-800 text-gray-500 dark:text-gray-400">
                  OU
                </span>
              </div>
            </div>

            {/* Botão Magic Link */}
            <button
              type="button"
              onClick={async () => {
                if (!formData.email.trim()) {
                  toast.error("Digite seu email primeiro");
                  return;
                }
                setLoading(true);
                try {
                  await authService.sendMagicLink(
                    formData.email,
                    "https://mediconnectbrasil.netlify.app/medico/painel"
                  );
                  toast.success(
                    "Link de acesso enviado para seu email! Verifique sua caixa de entrada.",
                    { duration: 6000 }
                  );
                } catch {
                  toast.error("Erro ao enviar link");
                } finally {
                  setLoading(false);
                }
              }}
              disabled={loading}
              className="w-full bg-white dark:bg-gray-700 text-indigo-700 dark:text-indigo-400 border-2 border-indigo-700 dark:border-indigo-400 py-3 px-4 rounded-lg font-medium hover:bg-indigo-50 dark:hover:bg-gray-600 hover:scale-105 active:scale-95 disabled:opacity-50 disabled:cursor-not-allowed disabled:hover:scale-100 transition-all duration-200 focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-indigo-500 focus-visible:ring-offset-2"
            >
              {loading ? "Enviando..." : "Entrar sem senha (Magic Link)"}
            </button>
          </form>
        </div>
      </div>
    </div>
  );
};

export default LoginMedico;
