"use client";

import { statusLabel } from "@/lib/display-format";

import {
  ShoppingCart,
  Package,
  Building2,
  Layers,
  Clock,
  Settings,
  Truck,
  CheckCircle2,
} from "lucide-react";
import type { MisReportSummaryDto } from "../dtos/mis-reporting.dto";

// ============================================
// Summary Card Component
// ============================================

interface SummaryCardProps {
  label: string;
  value: number;
  icon: React.ReactNode;
  color: string;
  bgColor: string;
  borderColor: string;
}

function SummaryCard({
  label,
  value,
  icon,
  color,
  bgColor,
  borderColor,
}: SummaryCardProps) {
  return (
    <div
      className={`rounded-xl border ${borderColor} ${bgColor} p-5 transition-all hover:shadow-md`}
    >
      <div className="flex items-center justify-between">
        <div>
          <p className="text-xs font-medium uppercase tracking-wider text-gray-500">
            {label}
          </p>
          <p className={`mt-1 text-2xl font-bold ${color}`}>
            {value.toLocaleString()}
          </p>
        </div>
        <div className={`rounded-lg p-2.5 ${bgColor} ${color}`}>{icon}</div>
      </div>
    </div>
  );
}

// ============================================
// Skeleton Loader
// ============================================

function SummarySkeleton() {
  return (
    <div className="grid grid-cols-2 gap-4 sm:grid-cols-4">
      {Array.from({ length: 8 }).map((_, i) => (
        <div
          key={i}
          className="rounded-xl border border-gray-200 bg-white p-5"
        >
          <div className="h-3 w-20 animate-pulse rounded bg-gray-200" />
          <div className="mt-2 h-7 w-16 animate-pulse rounded bg-gray-200" />
        </div>
      ))}
    </div>
  );
}

// ============================================
// MisReportSummary Component
// ============================================

interface MisReportSummaryProps {
  summary: MisReportSummaryDto | null;
  isLoading?: boolean;
}

export function MisReportSummary({
  summary,
  isLoading = false,
}: MisReportSummaryProps) {
  if (isLoading || !summary) {
    return <SummarySkeleton />;
  }

  const cards: SummaryCardProps[] = [
    {
      label: "Total Orders",
      value: summary.totalOrders,
      icon: <ShoppingCart className="h-5 w-5" />,
      color: "text-navy-900",
      bgColor: "bg-navy-50",
      borderColor: "border-navy-200",
    },
    {
      label: "Total Items",
      value: summary.totalItems,
      icon: <Package className="h-5 w-5" />,
      color: "text-blue-600",
      bgColor: "bg-blue-50",
      borderColor: "border-blue-200",
    },
    {
      label: `Total Quantity (${summary.unitLabel ?? "Unit not set"})`,
      value: summary.totalQuantity,
      icon: <Layers className="h-5 w-5" />,
      color: "text-purple-600",
      bgColor: "bg-purple-50",
      borderColor: "border-purple-200",
    },
    {
      label: "Companies ordering",
      value: summary.uniqueCompanies,
      icon: <Building2 className="h-5 w-5" />,
      color: "text-emerald-600",
      bgColor: "bg-emerald-50",
      borderColor: "border-emerald-200",
    },
    {
      label: statusLabel("ordered"),
      value: summary.orderedCount,
      icon: <Clock className="h-5 w-5" />,
      color: "text-amber-600",
      bgColor: "bg-amber-50",
      borderColor: "border-amber-200",
    },
    {
      label: statusLabel("processing"),
      value: summary.processingCount,
      icon: <Settings className="h-5 w-5" />,
      color: "text-blue-600",
      bgColor: "bg-blue-50",
      borderColor: "border-blue-200",
    },
    {
      label: statusLabel("dispatched"),
      value: summary.dispatchedCount,
      icon: <Truck className="h-5 w-5" />,
      color: "text-purple-600",
      bgColor: "bg-purple-50",
      borderColor: "border-purple-200",
    },
    {
      label: statusLabel("delivered"),
      value: summary.deliveredCount,
      icon: <CheckCircle2 className="h-5 w-5" />,
      color: "text-emerald-600",
      bgColor: "bg-emerald-50",
      borderColor: "border-emerald-200",
    },
  ];

  return (
    <div className="grid grid-cols-2 gap-4 sm:grid-cols-4">
      {cards.map((card) => (
        <SummaryCard key={card.label} {...card} />
      ))}
    </div>
  );
}
