import { BarChartCard, PieChartCard } from '@/components/charts';
import { Card, CardContent, CardHeader, CardTitle } from '@/components/ui/card';
import AppLayout from '@/layouts/app-layout';
import { type BreadcrumbItem } from '@/types';
import { Head } from '@inertiajs/react';
import { BarChart3, BookOpen, FolderKanban, Layers, Lightbulb, Target, Users } from 'lucide-react';

interface ChartDataItem {
    name: string;
    value: number;
}

interface ChartData {
    projectsByCategory: ChartDataItem[];
    projectsByArea: ChartDataItem[];
    projectsByEducationLevel: ChartDataItem[];
    projectsByMethodology: ChartDataItem[];
    projectsByTipoInnovacion: ChartDataItem[];
    projectsByNaturaleza: ChartDataItem[];
    projectsByContexto: ChartDataItem[];
    totals: {
        projects: number;
        categories: number;
        areas: number;
        educationLevels: number;
        methodologies: number;
        tiposInnovacion: number;
        naturalezas: number;
        contextos: number;
    };
}

interface DashboardProps {
    chartData: ChartData;
}

const breadcrumbs: BreadcrumbItem[] = [
    {
        title: 'Dashboard',
        href: '/dashboard',
    },
];

export default function Dashboard({ chartData }: DashboardProps) {
    const { totals } = chartData;

    return (
        <AppLayout breadcrumbs={breadcrumbs}>
            <Head title="Dashboard" />
            <div className="flex h-full flex-1 flex-col gap-6 rounded-xl p-4 overflow-x-auto">
                {/* KPIs / Tarjetas de resumen */}
                <div className="grid gap-4 md:grid-cols-2 lg:grid-cols-4">
                    <Card>
                        <CardHeader className="flex flex-row items-center justify-between space-y-0 pb-2">
                            <CardTitle className="text-sm font-medium">Total Proyectos</CardTitle>
                            <FolderKanban className="h-4 w-4 text-muted-foreground" />
                        </CardHeader>
                        <CardContent>
                            <div className="text-2xl font-bold">{totals.projects}</div>
                            <p className="text-xs text-muted-foreground">
                                Proyectos registrados
                            </p>
                        </CardContent>
                    </Card>
                    <Card>
                        <CardHeader className="flex flex-row items-center justify-between space-y-0 pb-2">
                            <CardTitle className="text-sm font-medium">Categorías</CardTitle>
                            <Layers className="h-4 w-4 text-muted-foreground" />
                        </CardHeader>
                        <CardContent>
                            <div className="text-2xl font-bold">{totals.categories}</div>
                            <p className="text-xs text-muted-foreground">
                                Categorías disponibles
                            </p>
                        </CardContent>
                    </Card>
                    <Card>
                        <CardHeader className="flex flex-row items-center justify-between space-y-0 pb-2">
                            <CardTitle className="text-sm font-medium">Áreas</CardTitle>
                            <Target className="h-4 w-4 text-muted-foreground" />
                        </CardHeader>
                        <CardContent>
                            <div className="text-2xl font-bold">{totals.areas}</div>
                            <p className="text-xs text-muted-foreground">
                                Áreas de conocimiento
                            </p>
                        </CardContent>
                    </Card>
                    <Card>
                        <CardHeader className="flex flex-row items-center justify-between space-y-0 pb-2">
                            <CardTitle className="text-sm font-medium">Niveles Educativos</CardTitle>
                            <BookOpen className="h-4 w-4 text-muted-foreground" />
                        </CardHeader>
                        <CardContent>
                            <div className="text-2xl font-bold">{totals.educationLevels}</div>
                            <p className="text-xs text-muted-foreground">
                                Niveles registrados
                            </p>
                        </CardContent>
                    </Card>
                </div>

                {/* Primera fila de gráficos: Categoría y Área (Pie charts) */}
                <div>
                    <PieChartCard
                        title="Proyectos por Área de Conocimiento"
                        description="Distribución por áreas de investigación"
                        data={chartData.projectsByArea}
                        totalProjects={totals.projects}
                    />
                </div>

                {/* Segunda fila: Nivel Educativo y Metodología (Bar charts horizontales) */}
                <div className="grid gap-4 md:grid-cols-2">
                    <BarChartCard
                        title="Proyectos por Nivel Educativo"
                        description="Distribución según nivel educativo"
                        data={chartData.projectsByEducationLevel}
                        color="hsl(142, 71%, 45%)"
                        horizontal={true}
                    />
                    <BarChartCard
                        title="Proyectos por Metodología"
                        description="Metodologías aplicadas en los proyectos"
                        data={chartData.projectsByMethodology}
                        color="hsl(262, 83%, 58%)"
                        horizontal={true}
                        totalProjects={totals.projects}
                    />
                </div>

                {/* Tercera fila: Tipo de Innovación (Pie) y Naturaleza (Bar) */}
                <div className="grid gap-4 md:grid-cols-2">
                    <PieChartCard
                        title="Proyectos por Tipo de Innovación"
                        description="Clasificación según tipo de innovación"
                        data={chartData.projectsByTipoInnovacion}
                        innerRadius={50}
                        outerRadius={90}
                        totalProjects={totals.projects}
                    />
                    <BarChartCard
                        title="Proyectos por Naturaleza"
                        description="Distribución según naturaleza del proyecto"
                        data={chartData.projectsByNaturaleza}
                        color="hsl(24, 95%, 53%)"
                        horizontal={true}
                        totalProjects={totals.projects}
                    />
                </div>

                {/* Cuarta fila: Contexto (Bar chart completo) */}
                <div className="grid gap-4">
                    <BarChartCard
                        title="Proyectos por Contexto"
                        description="Contextos de aplicación de los proyectos de investigación"
                        data={chartData.projectsByContexto}
                        color="hsl(199, 89%, 48%)"
                        horizontal={false}
                        totalProjects={totals.projects}
                    />
                </div>
            </div>
        </AppLayout >
    );
}
