"use client";

import { productLabel } from "@/lib/product-unit";

import { OrderStatusBadge as StatusBadge } from "@/components/order-status-badge";

import { displayValue, formatDate } from "@/lib/display-format";

import type { RecentOrderDto } from "../dtos/dashboard.dto";

interface RecentOrdersProps {
  orders: RecentOrderDto[];
  isLoading?: boolean;
  audienceRole?: string | null;
}



function SkeletonTable() {
  return (
    <div className="space-y-3">
      {Array.from({ length: 5 }).map((_, i) => (
        <div
          key={i}
          className="flex items-center gap-4 rounded-lg border border-gray-100 bg-gray-50 p-3"
        >
          <div className="h-4 w-20 animate-pulse rounded bg-gray-200" />
          <div className="h-4 w-28 animate-pulse rounded bg-gray-200" />
          <div className="h-4 w-24 animate-pulse rounded bg-gray-200" />
          <div className="ml-auto h-4 w-16 animate-pulse rounded bg-gray-200" />
        </div>
      ))}
    </div>
  );
}

export function RecentOrders({ orders, isLoading = false, audienceRole = null }: RecentOrdersProps) {
  return (
    <div className="rounded-xl border border-gray-200/80 bg-white p-6 shadow-sm">
      <div className="mb-4 flex items-center justify-between">
        <h2 className="text-base font-semibold text-navy-900">Recent Orders</h2>
        <span className="text-xs text-gray-400">
          Showing {orders.length} most recent
        </span>
      </div>

      {isLoading ? (
        <SkeletonTable />
      ) : (
        <div className="overflow-x-auto">
          <table className="w-full">
            <thead>
              <tr className="border-b border-gray-200 text-left">
                <th className="pb-3 text-xs font-medium uppercase tracking-wider text-gray-400">
                  Order ID
                </th>
                <th className="pb-3 text-xs font-medium uppercase tracking-wider text-gray-400">
                  Customer
                </th>
                <th className="pb-3 text-xs font-medium uppercase tracking-wider text-gray-400">
                  Product
                </th>
                <th className="pb-3 text-xs font-medium uppercase tracking-wider text-gray-400">
                  Company
                </th>
                <th className="pb-3 text-xs font-medium uppercase tracking-wider text-gray-400">
                  Status
                </th>
                <th className="pb-3 text-xs font-medium uppercase tracking-wider text-gray-400">
                  Date
                </th>
              </tr>
            </thead>
            <tbody className="divide-y divide-gray-100">
              {orders.map((order) => (
                <tr
                  key={order.orderId}
                  className="transition-colors hover:bg-gray-50"
                >
                  <td className="py-3 pr-4 text-sm font-mono text-gray-500">
                    {order.orderId.slice(0, 8)}
                  </td>
                  <td className="py-3 pr-4 text-sm font-medium text-navy-900">
                    {order.customerName}
                  </td>
                  <td className="py-3 pr-4 text-sm text-gray-600">
                    <div className="flex items-center gap-2 whitespace-nowrap">
                      <span>{productLabel(order.productName, order.defaultUnit)}</span>
                      {order.additionalItemsCount > 0 && (
                        <span
                          className="inline-flex rounded-full bg-navy-50 px-2 py-0.5 text-xs font-medium text-navy-700"
                          aria-label={`${order.additionalItemsCount} additional items`}
                        >
                          +{order.additionalItemsCount}
                        </span>
                      )}
                    </div>
                  </td>
                  <td className="py-3 pr-4 text-sm text-gray-600">
                    {displayValue(order.companyName)}
                  </td>
                  <td className="py-3 pr-4">
                    <StatusBadge status={order.status} audienceRole={audienceRole} />
                  </td>
                  <td className="whitespace-nowrap py-3 text-xs text-gray-400">
                    <time dateTime={order.createdAt ?? undefined}>{formatDate(order.createdAt)}</time>
                  </td>
                </tr>
              ))}
            </tbody>
          </table>
        </div>
      )}
    </div>
  );
}
