"use client";

import { useState, useEffect } from 'react';
import { Tab } from '@headlessui/react';
import { Button, Dialog } from '@/components/ui';
import { Toaster, toast } from 'react-hot-toast';
import {
    BuildingOfficeIcon,
    PhotoIcon,
    UserGroupIcon,
    ShieldCheckIcon,
    BellIcon,
    ArrowUpTrayIcon,
    TrashIcon,
    CheckCircleIcon,
} from '@heroicons/react/24/outline';

const tabs = [
    { name: 'Dados da Agência', icon: BuildingOfficeIcon },
    { name: 'Logo e Marca', icon: PhotoIcon },
    { name: 'Equipe', icon: UserGroupIcon },
    { name: 'Segurança', icon: ShieldCheckIcon },
    { name: 'Notificações', icon: BellIcon },
];

const parseAddressParts = (address: string) => {
    if (!address) return { street: '', number: '', complement: '' };

    const [streetPart, rest] = address.split(',', 2).map(part => part.trim());
    if (!rest) return { street: streetPart, number: '', complement: '' };

    const [numberPart, complementPart] = rest.split('-', 2).map(part => part.trim());
    return {
        street: streetPart,
        number: numberPart || '',
        complement: complementPart || '',
    };
};

export default function ConfiguracoesPage() {
    const [selectedTab, setSelectedTab] = useState(0);
    const [showSuccessDialog, setShowSuccessDialog] = useState(false);
    const [successMessage, setSuccessMessage] = useState('');
    const [showSupportDialog, setShowSupportDialog] = useState(false);
    const [supportMessage, setSupportMessage] = useState('Para alterar estes dados, contate o suporte.');
    const [loading, setLoading] = useState(true);
    const [loadingCep, setLoadingCep] = useState(false);
    const [uploadingLogo, setUploadingLogo] = useState(false);
    const [logoPreview, setLogoPreview] = useState<string | null>(null);
    const [logoHorizontalPreview, setLogoHorizontalPreview] = useState<string | null>(null);

    // Dados da agência (buscados da API)
    const [agencyData, setAgencyData] = useState({
        name: '',
        cnpj: '',
        email: '',
        phone: '',
        website: '',
        address: '',
        street: '',
        number: '',
        complement: '',
        neighborhood: '',
        city: '',
        state: '',
        zip: '',
        razaoSocial: '',
        description: '',
        industry: '',
        teamSize: '',
        logoUrl: '',
        logoHorizontalUrl: '',
    });

    // Dados para alteração de senha
    const [passwordData, setPasswordData] = useState({
        currentPassword: '',
        newPassword: '',
        confirmPassword: '',
    });

    useEffect(() => {
        const fetchAgencyData = async () => {
            try {
                setLoading(true);
                const token = localStorage.getItem('token');
                const userData = localStorage.getItem('user');

                if (!token || !userData) {
                    console.error('Usuário não autenticado');
                    setLoading(false);
                    return;
                }

                // Buscar dados da API
                const response = await fetch('/api/agency/profile', {
                    headers: {
                        'Authorization': `Bearer ${token}`,
                        'Content-Type': 'application/json',
                    },
                });

                if (response.ok) {
                    const data = await response.json();
                    console.log('DEBUG: API response data:', data);
                    console.log('DEBUG: logo_url:', data.logo_url);
                    console.log('DEBUG: logo_horizontal_url:', data.logo_horizontal_url);

                    const parsedAddress = parseAddressParts(data.address || '');
                    setAgencyData({
                        name: data.name || '',
                        cnpj: data.cnpj || '',
                        email: data.email || '',
                        phone: data.phone || '',
                        website: data.website || '',
                        address: data.address || '',
                        street: parsedAddress.street,
                        number: data.number || parsedAddress.number,
                        complement: data.complement || parsedAddress.complement,
                        neighborhood: data.neighborhood || '',
                        city: data.city || '',
                        state: data.state || '',
                        zip: data.zip || '',
                        razaoSocial: data.razao_social || '',
                        description: data.description || '',
                        industry: data.industry || '',
                        teamSize: data.team_size || '',
                        logoUrl: data.logo_url || '',
                        logoHorizontalUrl: data.logo_horizontal_url || '',
                    });

                    // Set logo previews
                    console.log('DEBUG: Setting previews...');
                    if (data.logo_url) {
                        console.log('DEBUG: Setting logoPreview to:', data.logo_url);
                        setLogoPreview(data.logo_url);
                    }
                    if (data.logo_horizontal_url) {
                        console.log('DEBUG: Setting logoHorizontalPreview to:', data.logo_horizontal_url);
                        setLogoHorizontalPreview(data.logo_horizontal_url);
                    }
                } else {
                    console.error('Erro ao buscar dados:', response.status);
                    // Fallback para localStorage se API falhar
                    const savedData = localStorage.getItem('cadastroData');
                    if (savedData) {
                        const data = JSON.parse(savedData);
                        const user = JSON.parse(userData);
                        setAgencyData({
                            name: data.formData?.companyName || '',
                            cnpj: data.formData?.cnpj || '',
                            email: data.formData?.email || user.email || '',
                            phone: data.contacts?.[0]?.phone || '',
                            website: data.formData?.website || '',
                            address: `${data.cepData?.logradouro || ''}, ${data.formData?.number || ''}`,
                            street: data.cepData?.logradouro || '',
                            number: data.formData?.number || '',
                            complement: data.formData?.complement || '',
                            neighborhood: data.cepData?.bairro || '',
                            city: data.cepData?.localidade || '',
                            state: data.cepData?.uf || '',
                            zip: data.formData?.cep || '',
                            razaoSocial: data.cnpjData?.razaoSocial || '',
                            description: data.formData?.description || '',
                            industry: data.formData?.industry || '',
                            teamSize: data.formData?.teamSize || '',
                            logoUrl: '',
                            logoHorizontalUrl: '',
                        });
                    }
                }
            } catch (error) {
                console.error('Erro ao buscar dados da agência:', error);
                setSuccessMessage('Erro ao carregar dados da agência.');
                setShowSuccessDialog(true);
            } finally {
                setLoading(false);
            }
        };

        fetchAgencyData();
    }, []);

    const handleLogoUpload = async (file: File, type: 'logo' | 'horizontal') => {
        if (!file) return;

        // Validar tipo de arquivo
        if (!file.type.startsWith('image/')) {
            toast.error('Por favor, selecione uma imagem válida');
            return;
        }

        // Validar tamanho (2MB)
        if (file.size > 2 * 1024 * 1024) {
            toast.error('A imagem deve ter no máximo 2MB');
            return;
        }

        setUploadingLogo(true);
        try {
            const token = localStorage.getItem('token');
            if (!token) {
                toast.error('Você precisa estar autenticado');
                return;
            }

            const formData = new FormData();
            formData.append('logo', file);
            formData.append('type', type);

            const response = await fetch('/api/agency/logo', {
                method: 'POST',
                headers: {
                    'Authorization': `Bearer ${token}`,
                },
                body: formData,
            });

            if (response.ok) {
                const data = await response.json();
                const logoUrl = data.logo_url || data.url;

                if (type === 'logo') {
                    setAgencyData(prev => ({ ...prev, logoUrl }));
                    setLogoPreview(logoUrl);
                } else {
                    setAgencyData(prev => ({ ...prev, logoHorizontalUrl: logoUrl }));
                    setLogoHorizontalPreview(logoUrl);
                }

                toast.success('Logo enviado com sucesso!');
            } else {
                const errorData = await response.json().catch(() => ({}));
                console.error('Upload error:', errorData);
                toast.error(errorData.error || 'Erro ao enviar logo. Tente novamente.');
            }
        } catch (error) {
            console.error('Erro ao fazer upload:', error);
            toast.error('Erro ao enviar logo. Verifique sua conexão.');
        } finally {
            setUploadingLogo(false);
        }
    };

    const handleFileSelect = (e: React.ChangeEvent<HTMLInputElement>, type: 'logo' | 'horizontal') => {
        const file = e.target.files?.[0];
        if (file) {
            // Create preview immediately
            const reader = new FileReader();
            reader.onloadend = () => {
                const previewUrl = reader.result as string;
                if (type === 'logo') {
                    setLogoPreview(previewUrl);
                } else {
                    setLogoHorizontalPreview(previewUrl);
                }
            };
            reader.readAsDataURL(file);

            // Upload file
            handleLogoUpload(file, type);
        }
    };

    const formatCep = (value: string) => {
        const numbers = value.replace(/\D/g, '');
        return numbers.replace(/(\d{5})(\d{0,3})/, '$1-$2').substring(0, 9);
    };

    const fetchCepData = async (cep: string) => {
        const numbers = cep.replace(/\D/g, '');
        if (numbers.length !== 8) return;

        setLoadingCep(true);
        try {
            const response = await fetch(`https://viacep.com.br/ws/${numbers}/json/`);
            if (!response.ok) {
                toast.error('Não foi possível consultar o CEP agora.');
                return;
            }

            const data = await response.json();
            if (data?.erro) {
                toast.error('CEP não encontrado. Verifique o número.');
                setAgencyData(prev => ({
                    ...prev,
                    address: '',
                    street: '',
                    neighborhood: '',
                    city: '',
                    state: '',
                }));
                return;
            }

            const formattedCep = formatCep(cep);
            const nextAddress = data.logradouro || '';
            const nextNeighborhood = data.bairro || '';
            const nextCity = data.localidade || '';
            const nextState = data.uf || '';

            setAgencyData(prev => ({
                ...prev,
                zip: formattedCep,
                address: nextAddress,
                street: nextAddress,
                neighborhood: nextNeighborhood,
                city: nextCity,
                state: nextState,
            }));

            toast.success('Endereço preenchido pelo CEP');
        } catch (error) {
            console.error('Erro ao buscar CEP:', error);
            toast.error('Erro ao buscar CEP. Tente novamente.');
        } finally {
            setLoadingCep(false);
        }
    };

    const handleSaveAgency = async () => {
        try {
            const token = localStorage.getItem('token');
            if (!token) {
                setSuccessMessage('Você precisa estar autenticado.');
                setShowSuccessDialog(true);
                return;
            }

            const composedAddress = agencyData.street
                ? `${agencyData.street}${agencyData.number ? `, ${agencyData.number}` : ''}${agencyData.complement ? ` - ${agencyData.complement}` : ''}`
                : agencyData.address;

            const response = await fetch('/api/agency/profile', {
                method: 'PUT',
                headers: {
                    'Authorization': `Bearer ${token}`,
                    'Content-Type': 'application/json',
                },
                body: JSON.stringify({
                    name: agencyData.name,
                    cnpj: agencyData.cnpj,
                    email: agencyData.email,
                    phone: agencyData.phone,
                    website: agencyData.website,
                    address: composedAddress,
                    neighborhood: agencyData.neighborhood,
                    number: agencyData.number,
                    complement: agencyData.complement,
                    city: agencyData.city,
                    state: agencyData.state,
                    zip: agencyData.zip,
                    razao_social: agencyData.razaoSocial,
                    description: agencyData.description,
                    industry: agencyData.industry,
                    team_size: agencyData.teamSize,
                }),
            });

            if (response.ok) {
                setSuccessMessage('Dados da agência salvos com sucesso!');
            } else {
                setSuccessMessage('Erro ao salvar dados. Tente novamente.');
            }
        } catch (error) {
            console.error('Erro ao salvar:', error);
            setSuccessMessage('Erro ao salvar dados. Verifique sua conexão.');
        }
        setShowSuccessDialog(true);
    };

    const handleChangePassword = async () => {
        // Validações
        if (!passwordData.currentPassword) {
            setSuccessMessage('Por favor, informe sua senha atual.');
            setShowSuccessDialog(true);
            return;
        }
        if (!passwordData.newPassword || passwordData.newPassword.length < 8) {
            setSuccessMessage('A nova senha deve ter pelo menos 8 caracteres.');
            setShowSuccessDialog(true);
            return;
        }
        if (passwordData.newPassword !== passwordData.confirmPassword) {
            setSuccessMessage('As senhas não coincidem.');
            setShowSuccessDialog(true);
            return;
        }

        try {
            const token = localStorage.getItem('token');
            if (!token) {
                setSuccessMessage('Você precisa estar autenticado.');
                setShowSuccessDialog(true);
                return;
            }

            const response = await fetch('/api/auth/change-password', {
                method: 'POST',
                headers: {
                    'Authorization': `Bearer ${token}`,
                    'Content-Type': 'application/json',
                },
                body: JSON.stringify({
                    currentPassword: passwordData.currentPassword,
                    newPassword: passwordData.newPassword,
                }),
            });

            if (response.ok) {
                setPasswordData({ currentPassword: '', newPassword: '', confirmPassword: '' });
                setSuccessMessage('Senha alterada com sucesso!');
            } else {
                const error = await response.text();
                setSuccessMessage(error || 'Erro ao alterar senha. Verifique sua senha atual.');
            }
        } catch (error) {
            console.error('Erro ao alterar senha:', error);
            setSuccessMessage('Erro ao alterar senha. Verifique sua conexão.');
        }
        setShowSuccessDialog(true);
    };

    return (
        <div className="p-6 max-w-7xl mx-auto">
            <Toaster position="top-center" />
            {/* Header */}
            <div className="mb-8">
                <h1 className="text-2xl font-bold text-gray-900 dark:text-white mb-2">
                    Configurações
                </h1>
                <p className="text-gray-600 dark:text-gray-400">
                    Gerencie as configurações da sua agência
                </p>
            </div>

            {/* Loading State */}
            {loading ? (
                <div className="flex items-center justify-center py-12">
                    <div className="animate-spin rounded-full h-12 w-12 border-b-2 border-gray-900 dark:border-gray-100"></div>
                </div>
            ) : (
                <>
                    {/* Tabs */}
                    <Tab.Group selectedIndex={selectedTab} onChange={setSelectedTab}>
                        <Tab.List className="flex space-x-1 rounded-xl bg-gray-100 dark:bg-gray-800 p-1 mb-8">
                            {tabs.map((tab) => {
                                const Icon = tab.icon;
                                return (
                                    <Tab
                                        key={tab.name}
                                        className={({ selected }) =>
                                            `w-full flex items-center justify-center space-x-2 rounded-lg py-2.5 text-sm font-medium leading-5 transition-all
                                    ${selected
                                                ? 'bg-white dark:bg-gray-900 text-gray-900 dark:text-white shadow'
                                                : 'text-gray-600 dark:text-gray-400 hover:bg-white/50 dark:hover:bg-gray-700/50 hover:text-gray-900 dark:hover:text-white'
                                            }`
                                        }
                                    >
                                        <Icon className="w-5 h-5" />
                                        <span className="hidden sm:inline">{tab.name}</span>
                                    </Tab>
                                );
                            })}
                        </Tab.List>

                        <Tab.Panels>
                            {/* Tab 1: Dados da Agência */}
                            <Tab.Panel className="space-y-6">
                                {/* Card: Informações da Agência */}
                                <div className="rounded-xl bg-white dark:bg-gray-800 p-6 border border-gray-200 dark:border-gray-700">
                                    <h2 className="text-lg font-semibold text-gray-900 dark:text-white mb-6">
                                        Informações da Agência
                                    </h2>

                                    <div className="grid grid-cols-1 md:grid-cols-2 gap-6">
                                        <div>
                                            <label className="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-2">
                                                Nome da Agência
                                            </label>
                                            <input
                                                type="text"
                                                value={agencyData.name}
                                                onChange={(e) => setAgencyData({ ...agencyData, name: e.target.value })}
                                                className="w-full px-4 py-2 border border-gray-300 dark:border-gray-600 rounded-lg bg-white dark:bg-gray-900 text-gray-900 dark:text-white focus:ring-2 focus:ring-gray-400 dark:focus:ring-gray-600 focus:outline-none"
                                            />
                                        </div>

                                        <div>
                                            <label className="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-2">
                                                Razão Social
                                            </label>
                                            <input
                                                type="text"
                                                value={agencyData.razaoSocial}
                                                onChange={(e) => setAgencyData({ ...agencyData, razaoSocial: e.target.value })}
                                                className="w-full px-4 py-2 border border-gray-300 dark:border-gray-600 rounded-lg bg-white dark:bg-gray-900 text-gray-900 dark:text-white focus:ring-2 focus:ring-gray-400 dark:focus:ring-gray-600 focus:outline-none"
                                            />
                                        </div>

                                        <div>
                                            <label className="text-sm font-medium text-gray-700 dark:text-gray-300 mb-2 flex items-center justify-between">
                                                <span>CNPJ</span>
                                                <span className="text-xs text-gray-500">Alteração via suporte</span>
                                            </label>
                                            <input
                                                type="text"
                                                value={agencyData.cnpj}
                                                readOnly
                                                onClick={() => {
                                                    setSupportMessage('Para alterar CNPJ, contate o suporte.');
                                                    setShowSupportDialog(true);
                                                }}
                                                className="w-full px-4 py-2 border border-gray-300 dark:border-gray-600 rounded-lg bg-gray-50 dark:bg-gray-800 text-gray-900 dark:text-white focus:outline-none cursor-pointer"
                                            />
                                        </div>

                                        <div>
                                            <label className="text-sm font-medium text-gray-700 dark:text-gray-300 mb-2 flex items-center justify-between">
                                                <span>E-mail (acesso)</span>
                                                <span className="text-xs text-gray-500">Alteração via suporte</span>
                                            </label>
                                            <input
                                                type="email"
                                                value={agencyData.email}
                                                readOnly
                                                onClick={() => {
                                                    setSupportMessage('Para alterar o e-mail de acesso, contate o suporte.');
                                                    setShowSupportDialog(true);
                                                }}
                                                className="w-full px-4 py-2 border border-gray-300 dark:border-gray-600 rounded-lg bg-gray-50 dark:bg-gray-800 text-gray-900 dark:text-white focus:outline-none cursor-pointer"
                                            />
                                        </div>

                                        <div>
                                            <label className="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-2">
                                                Telefone / WhatsApp
                                            </label>
                                            <input
                                                type="tel"
                                                value={agencyData.phone}
                                                onChange={(e) => setAgencyData({ ...agencyData, phone: e.target.value })}
                                                className="w-full px-4 py-2 border border-gray-300 dark:border-gray-600 rounded-lg bg-white dark:bg-gray-900 text-gray-900 dark:text-white focus:ring-2 focus:ring-gray-400 dark:focus:ring-gray-600 focus:outline-none"
                                            />
                                        </div>

                                        <div className="md:col-span-2">
                                            <label className="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-2">
                                                Website
                                            </label>
                                            <input
                                                type="url"
                                                value={agencyData.website}
                                                onChange={(e) => setAgencyData({ ...agencyData, website: e.target.value })}
                                                className="w-full px-4 py-2 border border-gray-300 dark:border-gray-600 rounded-lg bg-white dark:bg-gray-900 text-gray-900 dark:text-white focus:ring-2 focus:ring-gray-400 dark:focus:ring-gray-600 focus:outline-none"
                                            />
                                        </div>

                                        <div>
                                            <label className="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-2">
                                                Segmento / Indústria
                                            </label>
                                            <input
                                                type="text"
                                                value={agencyData.industry}
                                                onChange={(e) => setAgencyData({ ...agencyData, industry: e.target.value })}
                                                className="w-full px-4 py-2 border border-gray-300 dark:border-gray-600 rounded-lg bg-white dark:bg-gray-900 text-gray-900 dark:text-white focus:ring-2 focus:ring-gray-400 dark:focus:ring-gray-600 focus:outline-none"
                                            />
                                        </div>

                                        <div>
                                            <label className="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-2">
                                                Tamanho da Equipe
                                            </label>
                                            <input
                                                type="text"
                                                value={agencyData.teamSize}
                                                onChange={(e) => setAgencyData({ ...agencyData, teamSize: e.target.value })}
                                                className="w-full px-4 py-2 border border-gray-300 dark:border-gray-600 rounded-lg bg-white dark:bg-gray-900 text-gray-900 dark:text-white focus:ring-2 focus:ring-gray-400 dark:focus:ring-gray-600 focus:outline-none"
                                            />
                                        </div>
                                    </div>
                                </div>

                                {/* Card: Contato e Localização */}
                                <div className="rounded-xl bg-white dark:bg-gray-800 p-6 border border-gray-200 dark:border-gray-700">
                                    <h2 className="text-lg font-semibold text-gray-900 dark:text-white mb-6">
                                        Contato e Localização
                                    </h2>

                                    <div className="grid grid-cols-1 md:grid-cols-2 gap-6">

                                        <div>
                                            <label className="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-2">
                                                CEP
                                            </label>
                                            <input
                                                type="text"
                                                value={agencyData.zip}
                                                onChange={(e) => {
                                                    const formatted = formatCep(e.target.value);
                                                    setAgencyData(prev => ({ ...prev, zip: formatted }));

                                                    const numbers = formatted.replace(/\D/g, '');
                                                    if (numbers.length === 8) {
                                                        fetchCepData(formatted);
                                                    }

                                                    if (numbers.length === 0) {
                                                        setAgencyData(prev => ({
                                                            ...prev,
                                                            address: '',
                                                            street: '',
                                                            neighborhood: '',
                                                            city: '',
                                                            state: '',
                                                        }));
                                                    }
                                                }}
                                                className="w-full px-4 py-2 border border-gray-300 dark:border-gray-600 rounded-lg bg-white dark:bg-gray-900 text-gray-900 dark:text-white focus:ring-2 focus:ring-gray-400 dark:focus:ring-gray-600 focus:outline-none"
                                            />
                                        </div>

                                        <div>
                                            <label className="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-2">
                                                Estado
                                            </label>
                                            <input
                                                type="text"
                                                value={agencyData.state}
                                                readOnly
                                                className="w-full px-4 py-2 border border-gray-300 dark:border-gray-600 rounded-lg bg-gray-50 dark:bg-gray-800 text-gray-900 dark:text-white focus:outline-none cursor-not-allowed"
                                            />
                                        </div>                                                <div>
                                            <label className="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-2">
                                                Cidade
                                            </label>
                                            <input
                                                type="text"
                                                value={agencyData.city}
                                                readOnly
                                                className="w-full px-4 py-2 border border-gray-300 dark:border-gray-600 rounded-lg bg-gray-50 dark:bg-gray-800 text-gray-900 dark:text-white focus:outline-none cursor-not-allowed"
                                            />
                                        </div>

                                        <div>
                                            <label className="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-2">
                                                Bairro
                                            </label>
                                            <input
                                                type="text"
                                                value={agencyData.neighborhood}
                                                readOnly
                                                className="w-full px-4 py-2 border border-gray-300 dark:border-gray-600 rounded-lg bg-gray-50 dark:bg-gray-800 text-gray-900 dark:text-white focus:outline-none cursor-not-allowed"
                                            />
                                        </div>                                                <div className="md:col-span-2">
                                            <label className="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-2">
                                                Rua/Avenida
                                            </label>
                                            <input
                                                type="text"
                                                value={agencyData.street}
                                                readOnly
                                                className="w-full px-4 py-2 border border-gray-300 dark:border-gray-600 rounded-lg bg-gray-50 dark:bg-gray-800 text-gray-900 dark:text-white focus:outline-none cursor-not-allowed"
                                            />
                                        </div>                                        <div>
                                            <label className="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-2">
                                                Número
                                            </label>
                                            <input
                                                type="text"
                                                value={agencyData.number}
                                                onChange={(e) => setAgencyData({ ...agencyData, number: e.target.value })}
                                                className="w-full px-4 py-2 border border-gray-300 dark:border-gray-600 rounded-lg bg-white dark:bg-gray-900 text-gray-900 dark:text-white focus:ring-2 focus:ring-gray-400 dark:focus:ring-gray-600 focus:outline-none"
                                            />
                                        </div>

                                        <div>
                                            <label className="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-2">
                                                Complemento (opcional)
                                            </label>
                                            <input
                                                type="text"
                                                value={agencyData.complement}
                                                onChange={(e) => setAgencyData({ ...agencyData, complement: e.target.value })}
                                                className="w-full px-4 py-2 border border-gray-300 dark:border-gray-600 rounded-lg bg-white dark:bg-gray-900 text-gray-900 dark:text-white focus:ring-2 focus:ring-gray-400 dark:focus:ring-gray-600 focus:outline-none"
                                            />
                                        </div>
                                    </div>

                                    <div className="mt-6 flex justify-end">
                                        <button
                                            onClick={handleSaveAgency}
                                            className="px-6 py-2 rounded-lg text-white font-medium transition-all hover:scale-105"
                                            style={{ background: 'var(--gradient-primary)' }}
                                        >
                                            Salvar Alterações
                                        </button>
                                    </div>
                                </div>
                            </Tab.Panel>

                            {/* Tab 2: Logo e Marca */}
                            <Tab.Panel className="space-y-6">
                                <div className="rounded-xl bg-white dark:bg-gray-800 p-6 border border-gray-200 dark:border-gray-700">
                                    <div className="mb-6">
                                        <h2 className="text-lg font-semibold text-gray-900 dark:text-white">
                                            Logo e Identidade Visual
                                        </h2>
                                        <p className="mt-1 text-sm text-gray-500 dark:text-gray-400">
                                            Configure os logos da sua agência que serão exibidos no sistema
                                        </p>
                                    </div>

                                    <div className="grid grid-cols-1 lg:grid-cols-2 gap-8">
                                        {/* Logo Principal */}
                                        <div className="space-y-4">
                                            <div>
                                                <h3 className="text-sm font-semibold text-gray-900 dark:text-white mb-1">
                                                    Logo Principal (Quadrado)
                                                </h3>
                                                <p className="text-xs text-gray-500 dark:text-gray-400">
                                                    Usado no menu lateral e ícones do sistema
                                                </p>
                                            </div>

                                            <div className="relative">
                                                {logoPreview ? (
                                                    <div className="space-y-3">
                                                        <div className="relative border-2 border-gray-200 dark:border-gray-700 rounded-xl p-8 bg-linear-to-br from-gray-50 to-gray-100 dark:from-gray-900 dark:to-gray-800">
                                                            <div className="flex items-center justify-center min-h-[140px]">
                                                                <img
                                                                    src={logoPreview}
                                                                    alt="Logo Principal"
                                                                    className="max-h-36 max-w-full object-contain drop-shadow-lg"
                                                                />
                                                            </div>
                                                            {agencyData.logoUrl && (
                                                                <div className="absolute top-3 right-3">
                                                                    <div className="bg-green-100 dark:bg-green-900/30 text-green-700 dark:text-green-400 px-2 py-1 rounded-md flex items-center gap-1 text-xs font-medium">
                                                                        <CheckCircleIcon className="w-3.5 h-3.5" />
                                                                        Salvo
                                                                    </div>
                                                                </div>
                                                            )}
                                                        </div>
                                                        <div className="flex gap-2">
                                                            <label className="flex-1 cursor-pointer">
                                                                <input
                                                                    type="file"
                                                                    accept="image/png,image/jpeg,image/jpg,image/svg+xml"
                                                                    onChange={(e) => handleFileSelect(e, 'logo')}
                                                                    className="hidden"
                                                                    disabled={uploadingLogo}
                                                                />
                                                                <Button
                                                                    variant="primary"
                                                                    size="md"
                                                                    className="w-full pointer-events-none"
                                                                    disabled={uploadingLogo}
                                                                    isLoading={uploadingLogo}
                                                                    type="button"
                                                                >
                                                                    <ArrowUpTrayIcon className="w-4 h-4 mr-2" />
                                                                    {uploadingLogo ? 'Enviando...' : 'Trocar Logo'}
                                                                </Button>
                                                            </label>
                                                            <Button
                                                                variant="outline"
                                                                size="md"
                                                                onClick={() => {
                                                                    setLogoPreview(null);
                                                                    setAgencyData(prev => ({ ...prev, logoUrl: '' }));
                                                                }}
                                                                disabled={uploadingLogo}
                                                                className="border-red-200 dark:border-red-900 text-red-600 dark:text-red-400 hover:bg-red-50 dark:hover:bg-red-900/20"
                                                            >
                                                                <TrashIcon className="w-4 h-4 mr-2" />
                                                                Remover
                                                            </Button>
                                                        </div>
                                                    </div>
                                                ) : (
                                                    <label className="block cursor-pointer">
                                                        <input
                                                            type="file"
                                                            accept="image/png,image/jpeg,image/jpg,image/svg+xml"
                                                            onChange={(e) => handleFileSelect(e, 'logo')}
                                                            className="hidden"
                                                            disabled={uploadingLogo}
                                                        />
                                                        <div className="border-2 border-dashed border-gray-300 dark:border-gray-600 rounded-xl p-8 text-center hover:border-gray-400 dark:hover:border-gray-500 transition-colors bg-gray-50/50 dark:bg-gray-900/50">
                                                            <div className="flex flex-col items-center">
                                                                <div className="w-16 h-16 rounded-full bg-gray-100 dark:bg-gray-800 flex items-center justify-center mb-4">
                                                                    <PhotoIcon className="w-8 h-8 text-gray-400 dark:text-gray-500" />
                                                                </div>
                                                                <p className="text-sm font-medium text-gray-700 dark:text-gray-300 mb-1">
                                                                    Clique para fazer upload
                                                                </p>
                                                                <p className="text-xs text-gray-500 dark:text-gray-400 mb-4">
                                                                    ou arraste e solte aqui
                                                                </p>
                                                                <Button
                                                                    variant="primary"
                                                                    size="md"
                                                                    disabled={uploadingLogo}
                                                                    isLoading={uploadingLogo}
                                                                    type="button"
                                                                    className="pointer-events-none"
                                                                >
                                                                    <ArrowUpTrayIcon className="w-4 h-4 mr-2" />
                                                                    {uploadingLogo ? 'Enviando...' : 'Selecionar Arquivo'}
                                                                </Button>
                                                                <p className="text-xs text-gray-400 dark:text-gray-500 mt-3">
                                                                    PNG, JPG ou SVG • Máx. 2MB
                                                                </p>
                                                            </div>
                                                        </div>
                                                    </label>
                                                )}
                                            </div>
                                        </div>

                                        {/* Logo Horizontal */}
                                        <div className="space-y-4">
                                            <div>
                                                <h3 className="text-sm font-semibold text-gray-900 dark:text-white mb-1">
                                                    Logo Horizontal (Opcional)
                                                </h3>
                                                <p className="text-xs text-gray-500 dark:text-gray-400">
                                                    Usado no cabeçalho e emails
                                                </p>
                                            </div>

                                            <div className="relative">
                                                {logoHorizontalPreview ? (
                                                    <div className="space-y-3">
                                                        <div className="relative border-2 border-gray-200 dark:border-gray-700 rounded-xl p-8 bg-linear-to-br from-gray-50 to-gray-100 dark:from-gray-900 dark:to-gray-800">
                                                            <div className="flex items-center justify-center min-h-[140px]">
                                                                <img
                                                                    src={logoHorizontalPreview}
                                                                    alt="Logo Horizontal"
                                                                    className="max-h-36 max-w-full object-contain drop-shadow-lg"
                                                                />
                                                            </div>
                                                            {agencyData.logoHorizontalUrl && (
                                                                <div className="absolute top-3 right-3">
                                                                    <div className="bg-green-100 dark:bg-green-900/30 text-green-700 dark:text-green-400 px-2 py-1 rounded-md flex items-center gap-1 text-xs font-medium">
                                                                        <CheckCircleIcon className="w-3.5 h-3.5" />
                                                                        Salvo
                                                                    </div>
                                                                </div>
                                                            )}
                                                        </div>
                                                        <div className="flex gap-2">
                                                            <label className="flex-1 cursor-pointer">
                                                                <input
                                                                    type="file"
                                                                    accept="image/png,image/jpeg,image/jpg,image/svg+xml"
                                                                    onChange={(e) => handleFileSelect(e, 'horizontal')}
                                                                    className="hidden"
                                                                    disabled={uploadingLogo}
                                                                />
                                                                <Button
                                                                    variant="primary"
                                                                    size="md"
                                                                    className="w-full pointer-events-none"
                                                                    disabled={uploadingLogo}
                                                                    isLoading={uploadingLogo}
                                                                    type="button"
                                                                >
                                                                    <ArrowUpTrayIcon className="w-4 h-4 mr-2" />
                                                                    {uploadingLogo ? 'Enviando...' : 'Trocar Logo'}
                                                                </Button>
                                                            </label>
                                                            <Button
                                                                variant="outline"
                                                                size="md"
                                                                onClick={() => {
                                                                    setLogoHorizontalPreview(null);
                                                                    setAgencyData(prev => ({ ...prev, logoHorizontalUrl: '' }));
                                                                }}
                                                                disabled={uploadingLogo}
                                                                className="border-red-200 dark:border-red-900 text-red-600 dark:text-red-400 hover:bg-red-50 dark:hover:bg-red-900/20"
                                                            >
                                                                <TrashIcon className="w-4 h-4 mr-2" />
                                                                Remover
                                                            </Button>
                                                        </div>
                                                    </div>
                                                ) : (
                                                    <label className="block cursor-pointer">
                                                        <input
                                                            type="file"
                                                            accept="image/png,image/jpeg,image/jpg,image/svg+xml"
                                                            onChange={(e) => handleFileSelect(e, 'horizontal')}
                                                            className="hidden"
                                                            disabled={uploadingLogo}
                                                        />
                                                        <div className="border-2 border-dashed border-gray-300 dark:border-gray-600 rounded-xl p-8 text-center hover:border-gray-400 dark:hover:border-gray-500 transition-colors bg-gray-50/50 dark:bg-gray-900/50">
                                                            <div className="flex flex-col items-center">
                                                                <div className="w-16 h-16 rounded-full bg-gray-100 dark:bg-gray-800 flex items-center justify-center mb-4">
                                                                    <PhotoIcon className="w-8 h-8 text-gray-400 dark:text-gray-500" />
                                                                </div>
                                                                <p className="text-sm font-medium text-gray-700 dark:text-gray-300 mb-1">
                                                                    Clique para fazer upload
                                                                </p>
                                                                <p className="text-xs text-gray-500 dark:text-gray-400 mb-4">
                                                                    ou arraste e solte aqui
                                                                </p>
                                                                <Button
                                                                    variant="primary"
                                                                    size="md"
                                                                    disabled={uploadingLogo}
                                                                    isLoading={uploadingLogo}
                                                                    type="button"
                                                                    className="pointer-events-none"
                                                                >
                                                                    <ArrowUpTrayIcon className="w-4 h-4 mr-2" />
                                                                    {uploadingLogo ? 'Enviando...' : 'Selecionar Arquivo'}
                                                                </Button>
                                                                <p className="text-xs text-gray-400 dark:text-gray-500 mt-3">
                                                                    PNG, JPG ou SVG • Máx. 2MB
                                                                </p>
                                                            </div>
                                                        </div>
                                                    </label>
                                                )}
                                            </div>
                                        </div>
                                    </div>

                                    {/* Info adicional */}
                                    <div className="mt-6 p-4 bg-blue-50 dark:bg-blue-900/20 border border-blue-200 dark:border-blue-800 rounded-lg">
                                        <p className="text-sm text-blue-800 dark:text-blue-300">
                                            <strong>Dica:</strong> Para melhores resultados, use imagens de alta qualidade em formato PNG com fundo transparente.
                                        </p>
                                    </div>
                                </div>
                            </Tab.Panel>

                            {/* Tab 3: Equipe */}
                            <Tab.Panel className="rounded-xl bg-white dark:bg-gray-800 p-6 border border-gray-200 dark:border-gray-700">
                                <h2 className="text-lg font-semibold text-gray-900 dark:text-white mb-6">
                                    Gerenciamento de Equipe
                                </h2>

                                <div className="text-center py-12">
                                    <UserGroupIcon className="w-16 h-16 mx-auto text-gray-300 dark:text-gray-600 mb-4" />
                                    <p className="text-gray-600 dark:text-gray-400 mb-4">
                                        Em breve: gerenciamento completo de usuários e permissões
                                    </p>
                                    <button className="px-6 py-2 bg-gray-900 dark:bg-gray-100 text-white dark:text-gray-900 rounded-lg font-medium hover:scale-105 transition-all">
                                        Convidar Membro
                                    </button>
                                </div>
                            </Tab.Panel>

                            {/* Tab 3: Segurança */}
                            <Tab.Panel className="rounded-xl bg-white dark:bg-gray-800 p-6 border border-gray-200 dark:border-gray-700">
                                <h2 className="text-lg font-semibold text-gray-900 dark:text-white mb-6">
                                    Segurança e Privacidade
                                </h2>

                                {/* Alteração de Senha */}
                                <div className="max-w-2xl">
                                    <h3 className="text-md font-medium text-gray-900 dark:text-white mb-4">
                                        Alterar Senha
                                    </h3>

                                    <div className="space-y-4">
                                        <div>
                                            <label className="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-2">
                                                Senha Atual
                                            </label>
                                            <input
                                                type="password"
                                                value={passwordData.currentPassword}
                                                onChange={(e) => setPasswordData({ ...passwordData, currentPassword: e.target.value })}
                                                className="w-full px-4 py-2 border border-gray-300 dark:border-gray-600 rounded-lg bg-white dark:bg-gray-900 text-gray-900 dark:text-white focus:ring-2 focus:ring-gray-400 dark:focus:ring-gray-600 focus:outline-none"
                                                placeholder="Digite sua senha atual"
                                            />
                                        </div>

                                        <div>
                                            <label className="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-2">
                                                Nova Senha
                                            </label>
                                            <input
                                                type="password"
                                                value={passwordData.newPassword}
                                                onChange={(e) => setPasswordData({ ...passwordData, newPassword: e.target.value })}
                                                className="w-full px-4 py-2 border border-gray-300 dark:border-gray-600 rounded-lg bg-white dark:bg-gray-900 text-gray-900 dark:text-white focus:ring-2 focus:ring-gray-400 dark:focus:ring-gray-600 focus:outline-none"
                                                placeholder="Digite a nova senha (mínimo 8 caracteres)"
                                            />
                                        </div>

                                        <div>
                                            <label className="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-2">
                                                Confirmar Nova Senha
                                            </label>
                                            <input
                                                type="password"
                                                value={passwordData.confirmPassword}
                                                onChange={(e) => setPasswordData({ ...passwordData, confirmPassword: e.target.value })}
                                                className="w-full px-4 py-2 border border-gray-300 dark:border-gray-600 rounded-lg bg-white dark:bg-gray-900 text-gray-900 dark:text-white focus:ring-2 focus:ring-gray-400 dark:focus:ring-gray-600 focus:outline-none"
                                                placeholder="Digite a nova senha novamente"
                                            />
                                        </div>

                                        <div className="pt-4">
                                            <button
                                                onClick={handleChangePassword}
                                                className="px-6 py-2 rounded-lg text-white font-medium transition-all hover:scale-105"
                                                style={{ background: 'var(--gradient-primary)' }}
                                            >
                                                Alterar Senha
                                            </button>
                                        </div>
                                    </div>

                                    {/* Recursos Futuros */}
                                    <div className="mt-8 pt-8 border-t border-gray-200 dark:border-gray-700">
                                        <h3 className="text-md font-medium text-gray-900 dark:text-white mb-4">
                                            Recursos em Desenvolvimento
                                        </h3>
                                        <div className="space-y-3 text-sm text-gray-600 dark:text-gray-400">
                                            <div className="flex items-center space-x-2">
                                                <ShieldCheckIcon className="w-5 h-5" />
                                                <span>Autenticação em duas etapas (2FA)</span>
                                            </div>
                                            <div className="flex items-center space-x-2">
                                                <ShieldCheckIcon className="w-5 h-5" />
                                                <span>Histórico de acessos</span>
                                            </div>
                                            <div className="flex items-center space-x-2">
                                                <ShieldCheckIcon className="w-5 h-5" />
                                                <span>Dispositivos conectados</span>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                            </Tab.Panel>

                            {/* Tab 4: Notificações */}
                            <Tab.Panel className="rounded-xl bg-white dark:bg-gray-800 p-6 border border-gray-200 dark:border-gray-700">
                                <h2 className="text-lg font-semibold text-gray-900 dark:text-white mb-6">
                                    Preferências de Notificações
                                </h2>

                                <div className="text-center py-12">
                                    <BellIcon className="w-16 h-16 mx-auto text-gray-300 dark:text-gray-600 mb-4" />
                                    <p className="text-gray-600 dark:text-gray-400">
                                        Em breve: configuração de notificações por e-mail, push e mais
                                    </p>
                                </div>
                            </Tab.Panel>
                        </Tab.Panels>
                    </Tab.Group>
                </>
            )}

            {/* Dialog de Sucesso */}
            <Dialog
                isOpen={showSuccessDialog}
                onClose={() => setShowSuccessDialog(false)}
                title="Sucesso"
                size="sm"
            >
                <Dialog.Body>
                    <p className="text-center py-4">{successMessage}</p>
                </Dialog.Body>
                <Dialog.Footer>
                    <button
                        onClick={() => setShowSuccessDialog(false)}
                        className="px-6 py-2 rounded-lg text-white font-medium transition-all hover:scale-105"
                        style={{ background: 'var(--gradient-primary)' }}
                    >
                        OK
                    </button>
                </Dialog.Footer>
            </Dialog>

            {/* Dialog de Suporte */}
            <Dialog
                isOpen={showSupportDialog}
                onClose={() => setShowSupportDialog(false)}
                title="Contatar suporte"
            >
                <Dialog.Body>
                    <p className="text-sm text-gray-700 dark:text-gray-200">{supportMessage}</p>
                    <p className="mt-3 text-sm text-gray-500">Envie um e-mail para suporte@aggios.app ou abra um chamado para ajuste desses dados.</p>
                </Dialog.Body>
                <Dialog.Footer>
                    <button
                        onClick={() => setShowSupportDialog(false)}
                        className="px-4 py-2 bg-gray-900 dark:bg-gray-100 text-white dark:text-gray-900 rounded-lg font-medium hover:scale-105 transition-all"
                    >
                        Fechar
                    </button>
                </Dialog.Footer>
            </Dialog>
        </div>
    );
}
