import Link from "next/link";
import { cn } from "@/lib/utils";
import { TrendingUp, TrendingDown, Minus } from "lucide-react";

interface StatCardProps {
    label: string;
    value: string | number;
    icon: React.ElementType;
    color: string;        // e.g. "orange", "blue", "green", "purple"
    subtext?: string;
    trend?: number;        // percentage
    trendLabel?: string;
    href?: string;
}

const colorMap: Record<string, { bg: string; icon: string; ring: string }> = {
    orange: { bg: "bg-orange-50", icon: "text-orange-500", ring: "ring-orange-100" },
    blue: { bg: "bg-blue-50", icon: "text-blue-500", ring: "ring-blue-100" },
    green: { bg: "bg-emerald-50", icon: "text-emerald-500", ring: "ring-emerald-100" },
    purple: { bg: "bg-purple-50", icon: "text-purple-500", ring: "ring-purple-100" },
    red: { bg: "bg-red-50", icon: "text-red-500", ring: "ring-red-100" },
    amber: { bg: "bg-amber-50", icon: "text-amber-500", ring: "ring-amber-100" },
};

export function StatCard({
    label,
    value,
    icon: Icon,
    color,
    subtext,
    trend,
    trendLabel = "vs last period",
    href,
}: StatCardProps) {
    const colors = colorMap[color] || colorMap.blue;

    const content = (
        <div className={cn(
            "relative overflow-hidden rounded-2xl border border-slate-100 bg-white p-6 shadow-sm transition-all",
            href && "hover:shadow-md hover:border-slate-200 cursor-pointer group"
        )}>
            <div className="flex items-start justify-between">
                <div className="space-y-3">
                    <p className="text-sm font-medium text-slate-500">{label}</p>
                    <p className="text-3xl font-bold tracking-tight text-slate-900">
                        {typeof value === 'number' ? value.toLocaleString() : value}
                    </p>
                </div>
                <div className={cn(
                    "flex h-12 w-12 items-center justify-center rounded-xl ring-1",
                    colors.bg, colors.ring
                )}>
                    <Icon className={cn("h-6 w-6", colors.icon)} />
                </div>
            </div>

            <div className="mt-4 flex items-center justify-between">
                {trend !== undefined && (
                    <div className={cn(
                        "flex items-center gap-1 text-sm font-medium",
                        trend > 0 ? "text-emerald-600" : trend < 0 ? "text-red-600" : "text-slate-400"
                    )}>
                        {trend > 0 ? (
                            <TrendingUp className="h-3.5 w-3.5" />
                        ) : trend < 0 ? (
                            <TrendingDown className="h-3.5 w-3.5" />
                        ) : (
                            <Minus className="h-3.5 w-3.5" />
                        )}
                        <span>{trend > 0 ? "+" : ""}{trend}%</span>
                        <span className="font-normal text-slate-400">{trendLabel}</span>
                    </div>
                )}
                {subtext && !trend && (
                    <p className="text-sm text-slate-500">{subtext}</p>
                )}
                {subtext && trend !== undefined && (
                    <p className="text-xs text-slate-400">{subtext}</p>
                )}
            </div>

            {/* Decorative gradient */}
            <div className={cn(
                "absolute -right-6 -top-6 h-24 w-24 rounded-full opacity-[0.07]",
                colors.bg.replace("50", "500").replace("bg-", "bg-") // fallback
            )} style={{
                background: `radial-gradient(circle, ${color === 'orange' ? '#f97316' : color === 'blue' ? '#3b82f6' : color === 'green' ? '#10b981' : color === 'purple' ? '#8b5cf6' : '#6b7280'} 0%, transparent 70%)`
            }} />
        </div>
    );

    if (href) {
        return <Link href={href}>{content}</Link>;
    }

    return content;
}
