import React, { useState } from "react";
import { Mail, Lock, Clipboard } 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 LoginSecretaria: 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("[LoginSecretaria] Fazendo login com email:", formData.email);

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

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

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

      const userName =
        userInfo.profile?.full_name ||
        loginResponse.user.email?.split("@")[0] ||
        "Secretária";
      const roles = userInfo.roles || [];

      // Validar se tem permissão (admin, gestor ou secretaria)
      // Secretária pode ser paciente também, mas não médica
      const isAdmin = roles.includes("admin");
      const isGestor = roles.includes("gestor");
      const isSecretaria = roles.includes("secretaria");
      const isMedico = roles.includes("medico");

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

      // Secretária não pode ser médica (exceto se for admin/gestor)
      if (isSecretaria && isMedico && !isAdmin && !isGestor) {
        toast.error(
          "Usuário com múltiplas funções incompatíveis. Entre em contato com o suporte."
        );
        await authService.logout();
        setLoading(false);
        return;
      }

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

      if (ok) {
        console.log(
          "[LoginSecretaria] Login bem-sucedido! Navegando para /painel-secretaria"
        );
        toast.success(`Bem-vinda, ${userName}!`);
        navigate("/painel-secretaria");
      } else {
        console.error("[LoginSecretaria] loginComEmailSenha retornou false");
        toast.error("Erro ao processar login");
      }
    } catch (error: unknown) {
      console.error("[LoginSecretaria] 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-green-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-green-600 to-green-400 dark:from-green-700 dark:to-green-500 w-16 h-16 rounded-full flex items-center justify-center mx-auto mb-4 shadow-md">
            <Clipboard className="w-8 h-8 text-white" />
          </div>
          <h1 className="text-3xl font-bold text-gray-900 dark:text-gray-100 mb-2">
            Área da Secretaria
          </h1>
          <p className="text-gray-600 dark:text-gray-400">
            Faça login para acessar o sistema de gestão
          </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="sec_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="sec_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="sec_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="sec_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!");
                    } catch {
                      toast.error("Erro ao enviar email de recuperação");
                    }
                  }}
                  className="text-sm text-green-600 dark:text-green-400 hover:text-green-700 dark:hover:text-green-300 hover:underline transition-colors"
                >
                  Esqueceu a senha?
                </button>
              </div>
            </div>

            <button
              type="submit"
              disabled={loading}
              className="w-full bg-gradient-to-r from-green-600 to-green-400 text-white py-3 px-4 rounded-lg font-medium hover:from-green-700 hover:to-green-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-green-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/secretaria/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-green-700 dark:text-green-400 border-2 border-green-700 dark:border-green-400 py-3 px-4 rounded-lg font-medium hover:bg-green-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-green-500 focus-visible:ring-offset-2"
            >
              {loading ? "Enviando..." : "Entrar sem senha (Magic Link)"}
            </button>
          </form>
        </div>
      </div>
    </div>
  );
};

export default LoginSecretaria;
