import Link from 'next/link';

import { StoreOrderLineItem } from '@medusajs/types';
import { Eye } from 'lucide-react';

import { Trans } from '@kit/ui/makerkit/trans';

import type { AnalysisOrder, TTOOrder } from '~/lib/types/order';

import OrderItemsTable from './order-items-table';

export default function OrderBlock({
  analysisOrder,
  ttoLocation,
  ttoReservation,
  medusaOrderStatus,
  itemsAnalysisPackage,
  itemsTtoService,
  itemsOther,
  medusaOrderId,
}: {
  analysisOrder?: AnalysisOrder;
  ttoLocation?: { name: string };
  ttoReservation?: TTOOrder;
  medusaOrderStatus: string;
  itemsAnalysisPackage: StoreOrderLineItem[];
  itemsTtoService: StoreOrderLineItem[];
  itemsOther: StoreOrderLineItem[];
  medusaOrderId: string;
}) {
  return (
    <div className="flex flex-col gap-4">
      <h4>
        <Trans
          i18nKey="analysis-results:orderTitle"
          values={{ orderNumber: medusaOrderId }}
        />
      </h4>
      {analysisOrder && (
        <div className="flex gap-2">
          <h5>
            <Trans i18nKey={`orders:status.${analysisOrder.status}`} />
          </h5>
          <Link
            href={`/home/order/${analysisOrder.id}`}
            className="text-small-regular flex items-center justify-between"
          >
            <button className="text-ui-fg-subtle hover:text-ui-fg-base flex cursor-pointer gap-x-1">
              <Eye />
            </button>
          </Link>
        </div>
      )}
      <div className="flex flex-col gap-4">
        {analysisOrder && (
          <OrderItemsTable
            items={itemsAnalysisPackage}
            title="orders:table.analysisPackage"
            order={{
              medusaOrderId: analysisOrder.medusa_order_id,
              id: analysisOrder.id,
              status: analysisOrder.status,
            }}
          />
        )}
        {itemsTtoService && (
          <OrderItemsTable
            items={itemsTtoService}
            title="orders:table.ttoService"
            type="ttoService"
            order={{
              status: ttoReservation?.status,
              medusaOrderId,
              location: ttoLocation?.name,
              bookingCode: ttoReservation?.booking_code,
              clinicId: ttoReservation?.clinic_id,
              medusaLineItemId: ttoReservation?.medusa_cart_line_item_id,
            }}
          />
        )}
        <OrderItemsTable
          items={itemsOther}
          title="orders:table.otherOrders"
          order={{
            status: analysisOrder?.status,
          }}
        />
      </div>
    </div>
  );
}
