"use client";

import { useEffect, useState } from "react";
import { useRouter } from "next/navigation";
import Link from 'next/link';
import {
    BuildingOfficeIcon,
    UserGroupIcon,
    LinkIcon,
    ChartBarIcon,
    ArrowTrendingUpIcon,
    CheckCircleIcon,
    XCircleIcon,
} from '@heroicons/react/24/outline';

interface Agency {
    id: string;
    name: string;
    subdomain: string;
    is_active: boolean;
    created_at: string;
}

interface Stats {
    totalAgencies: number;
    activeAgencies: number;
    inactiveAgencies: number;
    totalUsers: number;
}

export default function SuperAdminDashboard() {
    const router = useRouter();
    const [loading, setLoading] = useState(true);
    const [agencies, setAgencies] = useState<Agency[]>([]);
    const [stats, setStats] = useState<Stats>({
        totalAgencies: 0,
        activeAgencies: 0,
        inactiveAgencies: 0,
        totalUsers: 0,
    });

    useEffect(() => {
        const token = localStorage.getItem('token');
        const userData = localStorage.getItem('user');

        if (!token || !userData) {
            router.push('/login');
            return;
        }

        const user = JSON.parse(userData);
        if (user.role !== 'SUPERADMIN') {
            localStorage.removeItem('token');
            localStorage.removeItem('user');
            router.push('/login');
            return;
        }

        loadData();
    }, [router]);

    const loadData = async () => {
        try {
            const response = await fetch('/api/admin/agencies', {
                headers: {
                    'Authorization': `Bearer ${localStorage.getItem('token')}`,
                },
            });

            if (response.ok) {
                const data = await response.json();
                setAgencies(data.slice(0, 5)); // Apenas as 5 primeiras

                // Calcular estatísticas
                setStats({
                    totalAgencies: data.length,
                    activeAgencies: data.filter((a: Agency) => a.is_active).length,
                    inactiveAgencies: data.filter((a: Agency) => !a.is_active).length,
                    totalUsers: data.length * 2, // Mock - implementar depois
                });
            }
        } catch (error) {
            console.error('Erro ao carregar dados:', error);
        } finally {
            setLoading(false);
        }
    };

    const statCards = [
        {
            name: 'Total de Agências',
            value: stats.totalAgencies,
            icon: BuildingOfficeIcon,
            color: 'orange',
            href: '/superadmin/agencies',
        },
        {
            name: 'Agências Ativas',
            value: stats.activeAgencies,
            icon: CheckCircleIcon,
            color: 'green',
            href: '/superadmin/agencies',
        },
        {
            name: 'Links de Cadastro',
            value: '5', // Mock
            icon: LinkIcon,
            color: 'pink',
            href: '/superadmin/signup-templates',
        },
        {
            name: 'Total de Usuários',
            value: stats.totalUsers,
            icon: UserGroupIcon,
            color: 'rose',
            href: '/superadmin/users',
        },
    ];

    if (loading) {
        return (
            <div className="flex items-center justify-center h-full p-8">
                <div className="animate-spin rounded-full h-12 w-12 border-b-2 border-purple-600"></div>
            </div>
        );
    }

    return (
        <div className="p-6 h-full overflow-auto">
            <div className="space-y-6">
                {/* Header */}
                <div>
                    <h1 className="text-2xl font-bold text-gray-900 dark:text-white">
                        Dashboard
                    </h1>
                    <p className="mt-1 text-sm text-gray-600 dark:text-gray-400">
                        Visão geral da plataforma Aggios
                    </p>
                </div>

                {/* Stats Grid */}
                <div className="grid grid-cols-1 gap-4 sm:grid-cols-2 lg:grid-cols-4">
                    {statCards.map((stat) => {
                        const Icon = stat.icon;
                        return (
                            <Link
                                key={stat.name}
                                href={stat.href}
                                className="group relative overflow-hidden rounded-xl bg-white dark:bg-gray-900 p-4 border border-gray-200 dark:border-gray-800 transition-all"
                            >
                                <div className="flex items-center justify-between">
                                    <div>
                                        <p className="text-xs font-medium text-gray-600 dark:text-gray-400">
                                            {stat.name}
                                        </p>
                                        <p className="mt-1 text-2xl font-bold text-gray-900 dark:text-white">
                                            {stat.value}
                                        </p>
                                    </div>
                                    <div
                                        className={`rounded-lg p-2 bg-${stat.color}-100 dark:bg-${stat.color}-900/20`}
                                    >
                                        <Icon
                                            className={`h-5 w-5 text-${stat.color}-600 dark:text-${stat.color}-400`}
                                        />
                                    </div>
                                </div>
                            </Link>
                        );
                    })}
                </div>

                {/* Recent Agencies */}
                <div className="rounded-xl bg-white dark:bg-gray-900 border border-gray-200 dark:border-gray-800">
                    <div className="px-4 py-3 border-b border-gray-200 dark:border-gray-800">
                        <div className="flex items-center justify-between">
                            <h2 className="text-base font-semibold text-gray-900 dark:text-white">
                                Agências Recentes
                            </h2>
                            <Link
                                href="/superadmin/agencies"
                                className="text-xs font-medium text-purple-600 hover:text-purple-500 dark:text-purple-400"
                            >
                                Ver todas →
                            </Link>
                        </div>
                    </div>
                    <div className="divide-y divide-gray-200 dark:divide-gray-800">
                        {agencies.length === 0 ? (
                            <div className="px-4 py-8 text-center">
                                <BuildingOfficeIcon className="mx-auto h-10 w-10 text-gray-400" />
                                <h3 className="mt-2 text-sm font-medium text-gray-900 dark:text-white">
                                    Nenhuma agência
                                </h3>
                                <p className="mt-1 text-xs text-gray-500 dark:text-gray-400">
                                    Comece criando uma nova agência.
                                </p>
                            </div>
                        ) : (
                            agencies.map((agency) => (
                                <div
                                    key={agency.id}
                                    className="px-4 py-3 hover:bg-gray-50 dark:hover:bg-gray-800 transition-colors"
                                >
                                    <div className="flex items-center justify-between">
                                        <div className="flex items-center gap-3">
                                            <div className="h-8 w-8 rounded-lg flex items-center justify-center" style={{ background: 'var(--gradient)' }}>
                                                <span className="text-xs font-medium text-white">
                                                    {agency.name.charAt(0).toUpperCase()}
                                                </span>
                                            </div>
                                            <div>
                                                <h3 className="text-sm font-medium text-gray-900 dark:text-white">
                                                    {agency.name}
                                                </h3>
                                                <p className="text-xs text-gray-500 dark:text-gray-400">
                                                    {agency.subdomain}.aggios.app
                                                </p>
                                            </div>
                                        </div>
                                        <div className="flex items-center gap-2">
                                            {agency.is_active ? (
                                                <span className="inline-flex items-center gap-1 rounded-full bg-green-100 dark:bg-green-900/20 px-2 py-0.5 text-[10px] font-medium text-green-800 dark:text-green-400">
                                                    <CheckCircleIcon className="h-3 w-3" />
                                                    Ativo
                                                </span>
                                            ) : (
                                                <span className="inline-flex items-center gap-1 rounded-full bg-red-100 dark:bg-red-900/20 px-2 py-0.5 text-[10px] font-medium text-red-800 dark:text-red-400">
                                                    <XCircleIcon className="h-3 w-3" />
                                                    Inativo
                                                </span>
                                            )}
                                            <span className="text-xs text-gray-500 dark:text-gray-400">
                                                {new Date(agency.created_at).toLocaleDateString('pt-BR')}
                                            </span>
                                        </div>
                                    </div>
                                </div>
                            ))
                        )}
                    </div>
                </div>

                {/* Quick Actions */}
                <div className="grid grid-cols-1 gap-4 sm:grid-cols-3">
                    <Link
                        href="/superadmin/agencies"
                        className="group relative overflow-hidden rounded-xl p-4 transition-all"
                        style={{ background: 'var(--gradient)' }}
                    >
                        <div className="flex items-center gap-3 text-white">
                            <BuildingOfficeIcon className="h-6 w-6" />
                            <div>
                                <h3 className="font-semibold text-sm">Gerenciar Agências</h3>
                                <p className="text-xs text-white/80">Ver e editar agências</p>
                            </div>
                        </div>
                    </Link>

                    <Link
                        href="/superadmin/signup-templates"
                        className="group relative overflow-hidden rounded-xl bg-gradient-to-r from-orange-500 to-pink-600 p-4 transition-all"
                    >
                        <div className="flex items-center gap-3 text-white">
                            <LinkIcon className="h-6 w-6" />
                            <div>
                                <h3 className="font-semibold text-sm">Links de Cadastro</h3>
                                <p className="text-xs text-white/80">Criar links personalizados</p>
                            </div>
                        </div>
                    </Link>

                    <Link
                        href="/superadmin/reports"
                        className="group relative overflow-hidden rounded-xl bg-gradient-to-r from-pink-500 to-rose-600 p-4 transition-all"
                    >
                        <div className="flex items-center gap-3 text-white">
                            <ChartBarIcon className="h-6 w-6" />
                            <div>
                                <h3 className="font-semibold text-sm">Relatórios</h3>
                                <p className="text-xs text-white/80">Análises e métricas</p>
                            </div>
                        </div>
                    </Link>
                </div>
            </div>
        </div>
    );
}
