import { cn } from "@/lib/utils";

interface MiniChartProps {
    title: string;
    data: { label: string; values: { name: string; value: number; color: string }[] }[];
    height?: number;
}

export function MiniChart({ title, data, height = 160 }: MiniChartProps) {
    if (data.length === 0) {
        return (
            <div className="rounded-2xl border border-slate-100 bg-white p-6 shadow-sm">
                <h3 className="text-sm font-semibold text-slate-700">{title}</h3>
                <div className="flex items-center justify-center" style={{ height }}>
                    <p className="text-sm text-slate-400">No data available</p>
                </div>
            </div>
        );
    }

    // Find the max value for scaling
    const maxValue = Math.max(
        ...data.flatMap((d) => d.values.map((v) => v.value)),
        1 // prevent division by zero
    );

    // Collect unique legend entries
    const legendItems = data[0]?.values.map((v) => ({ name: v.name, color: v.color })) || [];

    return (
        <div className="rounded-2xl border border-slate-100 bg-white p-6 shadow-sm">
            {/* Header */}
            <div className="mb-4 flex items-center justify-between">
                <h3 className="text-sm font-semibold text-slate-700">{title}</h3>
                <div className="flex items-center gap-3">
                    {legendItems.map((item) => (
                        <div key={item.name} className="flex items-center gap-1.5">
                            <div
                                className="h-2.5 w-2.5 rounded-full"
                                style={{ backgroundColor: item.color }}
                            />
                            <span className="text-[11px] text-slate-500">{item.name}</span>
                        </div>
                    ))}
                </div>
            </div>

            {/* Chart */}
            <div className="flex items-end gap-1" style={{ height }}>
                {data.map((point, idx) => (
                    <div key={idx} className="group relative flex flex-1 flex-col items-center gap-1">
                        {/* Bars */}
                        <div className="flex w-full items-end justify-center gap-0.5" style={{ height: height - 24 }}>
                            {point.values.map((v) => {
                                const barHeight = maxValue > 0 ? (v.value / maxValue) * 100 : 0;
                                return (
                                    <div
                                        key={v.name}
                                        className="w-full max-w-3 rounded-t-sm transition-all group-hover:opacity-80"
                                        style={{
                                            height: `${Math.max(barHeight, 2)}%`,
                                            backgroundColor: v.color,
                                            minHeight: v.value > 0 ? '4px' : '0px',
                                        }}
                                    />
                                );
                            })}
                        </div>

                        {/* Label */}
                        <span className="text-[10px] text-slate-400">{point.label}</span>

                        {/* Tooltip on hover */}
                        <div className="pointer-events-none absolute -top-10 left-1/2 z-10 -translate-x-1/2 whitespace-nowrap rounded-md bg-slate-900 px-2 py-1 text-[10px] text-white opacity-0 shadow-lg group-hover:opacity-100 transition-opacity">
                            {point.values.map((v) => `${v.name}: ${v.value}`).join(' · ')}
                        </div>
                    </div>
                ))}
            </div>
        </div>
    );
}
