import React, { useState, useEffect } from 'react';
import { Mail, Calendar, Clock, Upload, CircleAlert, CircleCheckBig, Truck, LayoutDashboard, Key, ShieldCheck, FileSearch, Trash2, Loader2, Settings } from 'lucide-react';
import { useGrOps } from '../hooks/useGrOps';
import * as grService from '../services/grService';
import { useAuthContext } from '@/components/shared/AuthProvider';
import { toast } from 'sonner';
import { handleGrError } from '../utils/grErrorHandler';
const GrOutlookView = () => {


  const { userData } = useAuthContext();
  const user = userData?.usuario || { username: '', email: '' };
  
  const [dataCorte, setDataCorte] = useState(new Date().toISOString().split('T')[0]);
  const [assuntoFiltro, setAssuntoFiltro] = useState('NFe');
  const [isProcessing, setIsProcessing] = useState(false);
  const [results, setResults] = useState(null);

  // Email Credentials State
  const [emailCred, setEmailCred] = useState(user.email || '');
  const [passwordCred, setPasswordCred] = useState('');
  const [isSavingCreds, setIsSavingCreds] = useState(false);

  // XML Validation State
  const [isValidating, setIsValidating] = useState(false);
  const [validationResult, setValidationResult] = useState(null);

  const handleSaveCredentials = async () => {
    if (!emailCred || !passwordCred) {
      toast.error("Preencha e-mail e senha");
      return;
    }
    setIsSavingCreds(true);
    try {
      await grService.saveEmailCredentials(emailCred, passwordCred);
      toast.success("Credenciais salvas com sucesso");
      setPasswordCred('');
    } catch (error) {
      handleGrError(error, "Erro ao salvar credenciais");
    } finally {
      setIsSavingCreds(false);
    }
  };

  const handleProcessEmails = async () => {
    setIsProcessing(true);
    setResults(null);
    try {
      const resp = await grService.processEmails({
        desde_data: dataCorte,
        assunto_filtro: assuntoFiltro,
        salvar_xmls: true
      });
      setResults(resp);
      toast.success(`${resp.total_xmls} XMLs processados com sucesso`);
    } catch (error) {
      handleGrError(error, "Erro ao processar e-mails");
    } finally {
      setIsProcessing(false);
    }
  };

  const handleValidateXML = async (e) => {
    const file = e.target.files[0];
    if (!file) return;

    setIsValidating(true);
    setValidationResult(null);
    try {
      const resp = await grService.validateXML(file);
      setValidationResult(resp);
      if (resp.valido) {
        toast.success("XML válido!");
      } else {
        toast.error("XML inválido para o sistema");
      }
    } catch (error) {
      handleGrError(error, "Erro ao validar XML");
    } finally {
      setIsValidating(false);
    }
  };

  return (
    <div className="min-h-[80vh] flex items-center justify-center p-4 md:p-8 animate-in fade-in zoom-in-95 duration-500">
      <div className="w-full max-w-[540px] bg-white dark:bg-[#1a1a1a] rounded-2xl border border-slate-200 dark:border-white/5 shadow-2xl overflow-hidden relative">
        {/* Top Accent Line */}
        <div className="absolute top-0 left-0 right-0 h-1 bg-gradient-to-r from-transparent via-[var(--gr-primary)] to-transparent opacity-50" />

        <header className="p-8 pb-4 space-y-2 text-center">
          <div className="mx-auto w-12 h-12 bg-[var(--gr-primary)]/10 rounded-xl flex items-center justify-center mb-4">
            <Mail className="text-[var(--gr-primary)]" size={24} />
          </div>
          <h1 className="text-2xl font-black text-slate-800 dark:text-white tracking-tight">
            Central de <span className="text-[var(--gr-primary)]">Mensagens</span>
          </h1>
          <p className="text-[10px] font-bold text-slate-400 uppercase tracking-[0.2em]">
            Ambiente Seguro de Processamento
          </p>
        </header>

        <main className="p-8 pt-4 space-y-6">
          {/* Main Controls Overlay */}
          <div className="space-y-4">
            <div className="grid grid-cols-1 gap-4">
              <div className="space-y-1.5">
                <label className="text-[9px] font-bold text-slate-400 uppercase tracking-widest ml-1">Processar Desde</label>
                <div className="relative group">
                  <Calendar className="absolute left-3 top-1/2 -translate-y-1/2 text-slate-400 group-focus-within:text-[var(--gr-primary)] transition-colors" size={14} />
                  <input 
                    type="date" 
                    value={dataCorte}
                    onChange={(e) => setDataCorte(e.target.value)}
                    className="w-full pl-9 pr-4 py-2.5 bg-slate-50 dark:bg-[#222] border border-slate-200 dark:border-white/10 rounded-xl text-xs font-semibold focus:outline-none focus:border-[var(--gr-primary)] focus:ring-4 focus:ring-[var(--gr-primary)]/5 transition-all"
                  />
                </div>
              </div>

              <div className="space-y-1.5">
                <label className="text-[9px] font-bold text-slate-400 uppercase tracking-widest ml-1">Filtro de Conteúdo</label>
                <div className="relative group">
                  <FileSearch className="absolute left-3 top-1/2 -translate-y-1/2 text-slate-400 group-focus-within:text-[var(--gr-primary)] transition-colors" size={14} />
                  <input 
                    type="text" 
                    value={assuntoFiltro}
                    onChange={(e) => setAssuntoFiltro(e.target.value)}
                    className="w-full pl-9 pr-4 py-2.5 bg-slate-50 dark:bg-[#222] border border-slate-200 dark:border-white/10 rounded-xl text-xs font-semibold focus:outline-none focus:border-[var(--gr-primary)] focus:ring-4 focus:ring-[var(--gr-primary)]/5 transition-all"
                    placeholder="Ex: NFe, Fatura..."
                  />
                </div>
              </div>
            </div>

            <button 
              onClick={handleProcessEmails}
              disabled={isProcessing}
              className="w-full flex items-center justify-center gap-3 px-6 py-4 bg-[var(--gr-primary)] hover:bg-[var(--gr-primary-dark)] text-white rounded-xl font-bold text-xs uppercase tracking-[0.15em] transition-all shadow-lg shadow-[var(--gr-primary)]/10 disabled:opacity-50 hover:scale-[1.01] active:scale-[0.98]"
            >
              {isProcessing ? <Loader2 className="animate-spin" size={18} /> : <Upload size={18} />}
              Sincronizar Mensagens
            </button>
          </div>

          <div className="h-px bg-slate-100 dark:bg-white/5 mx-[-2rem]" />

          {/* Secondary Actions */}
          <div className="flex items-center gap-4">
            <label className="flex-1 flex items-center gap-3 p-3 bg-slate-50 dark:bg-[#222] border border-slate-200 dark:border-white/5 rounded-xl cursor-pointer hover:border-[var(--gr-primary)] hover:bg-[var(--gr-primary)]/5 transition-all group">
              <div className="w-8 h-8 rounded-lg bg-white dark:bg-[#2a2a2a] flex items-center justify-center border border-slate-200 dark:border-white/10 group-hover:bg-[var(--gr-primary)] transition-colors">
                <Upload className="text-slate-400 group-hover:text-white transition-colors" size={14} />
              </div>
              <div className="flex flex-col">
                <span className="text-[10px] font-bold text-slate-700 dark:text-slate-300">Validar XML</span>
                <span className="text-[8px] text-slate-400 font-medium">Upload manual alternativo</span>
              </div>
              <input type="file" className="hidden" accept=".xml" onChange={handleValidateXML} disabled={isValidating} />
            </label>

            {validationResult && (
              <div className={`flex items-center gap-2 p-2 px-3 rounded-xl border animate-in fade-in zoom-in-95 ${validationResult.valido ? 'bg-emerald-500/10 text-emerald-600 border-emerald-500/20' : 'bg-red-500/10 text-red-600 border-red-500/20'}`}>
                {validationResult.valido ? <CircleCheckBig size={14} /> : <CircleAlert size={14} />}
                <span className="text-[9px] font-black uppercase tracking-tighter truncate max-w-[80px]">{validationResult.valido ? 'OK' : 'ERRO'}</span>
              </div>
            )}
          </div>

          {/* Expandable Results Area */}
          {(results || isProcessing) && (
            <div className="pt-2 animate-in slide-in-from-top-4 duration-500">
              <div className="bg-slate-50 dark:bg-[#141414] rounded-xl border border-slate-200 dark:border-white/5 p-4 space-y-4">
                <div className="flex items-center justify-between border-b border-slate-200 dark:border-white/5 pb-3">
                  <h3 className="text-[10px] font-black text-slate-400 uppercase tracking-widest">Fluxo de Dados</h3>
                  {results && (
                    <span className="text-[8px] font-bold bg-emerald-500/20 text-emerald-600 px-2 py-0.5 rounded-full border border-emerald-500/20">
                      CONCLUÍDO
                    </span>
                  )}
                </div>

                {isProcessing ? (
                  <div className="py-4 flex flex-col items-center justify-center space-y-3 opacity-60">
                    <Loader2 className="animate-spin text-[var(--gr-primary)]" size={24} />
                    <p className="text-[9px] font-bold text-slate-500 uppercase tracking-widest">Processando Mensagens...</p>
                  </div>
                ) : results && (
                  <div className="space-y-4">
                    <div className="grid grid-cols-2 gap-2">
                       <div className="bg-white dark:bg-[#1a1a1a] p-3 rounded-lg border border-slate-200 dark:border-white/10">
                          <span className="text-[8px] font-bold text-slate-400 uppercase block mb-1">Total XMLs</span>
                          <span className="text-xl font-black text-slate-800 dark:text-white">{results.total_xmls}</span>
                       </div>
                       <div className="bg-white dark:bg-[#1a1a1a] p-3 rounded-lg border border-slate-200 dark:border-white/10 overflow-hidden">
                          <span className="text-[8px] font-bold text-slate-400 uppercase block mb-1">Conta</span>
                          <span className="text-[9px] font-bold text-slate-600 dark:text-slate-400 truncate block">{results.email_processado}</span>
                       </div>
                    </div>

                    <div className="space-y-1.5 max-h-[160px] overflow-y-auto custom-scrollbar pr-2">
                      {results.resultados.map((res, i) => (
                        <div key={i} className="flex items-center justify-between bg-white dark:bg-[#1a1a1a] p-2.5 rounded-lg border border-slate-200 dark:border-white/10 group">
                          <div className="flex items-center gap-2 min-w-0">
                            <Truck size={12} className="text-slate-400" />
                            <span className="text-[9px] font-medium text-slate-500 dark:text-slate-400 truncate">{res.arquivo}</span>
                          </div>
                          <CircleCheckBig size={12} className="text-emerald-500 opacity-60 flex-shrink-0" />
                        </div>
                      ))}
                    </div>
                  </div>
                )}
              </div>
            </div>
          )}
        </main>

        <footer className="bg-slate-50 dark:bg-[#141414] p-4 text-center">
          <div className="flex items-center justify-center gap-2 opacity-30 group hover:opacity-100 transition-opacity">
            <ShieldCheck size={12} className="text-[var(--gr-primary)]" />
            <span className="text-[8px] font-bold text-slate-500 uppercase tracking-[0.3em]">Criptografia GR 256-bit</span>
          </div>
        </footer>
      </div>
    </div>
  );
};

export default GrOutlookView;
