import Card from '@/components/Card';
import Table from '@/components/Table';
import { formatNumber } from '@/lib/helper';
import { useTableFilter } from '@/services/hooks/useTableFilter';
import { ProductWarehouseStock } from '@/types/api/inventory/product';
import { ColumnDef } from '@tanstack/react-table';

const stockProductWarehouseTableColumns: ColumnDef<ProductWarehouseStock>[] = [
  {
    header: 'Nama Gudang',
    accessorKey: 'warehouse_name',
  },
  {
    header: 'Lokasi',
    accessorKey: 'location',
    cell: (props) => {
      return props.row.original.location != null
        ? props.row.original.location.name
        : '-';
    },
  },
  {
    header: 'Stok',
    accessorFn(row) {
      return row.current_stock;
    },
    cell: (props) => {
      return formatNumber(props.row.original.current_stock);
    },
  },
];

const StockProductWarehouseTable = ({
  productWarehouseStock,
}: {
  productWarehouseStock?: ProductWarehouseStock[];
}) => {
  const { state: tableFilterState, setPage, setPageSize } = useTableFilter();

  return (
    <Card
      title='Informasi Gudang'
      collapsible
      variant='bordered'
      className={{
        wrapper: 'w-full',
      }}
    >
      <Table<ProductWarehouseStock>
        data={productWarehouseStock ?? []}
        columns={stockProductWarehouseTableColumns}
        pageSize={tableFilterState.pageSize}
        page={tableFilterState.page ?? 0}
        totalItems={productWarehouseStock?.length ?? 0}
        onPageChange={setPage}
        onPageSizeChange={setPageSize}
        className={{
          containerClassName: 'mt-6 mb-0',
          tableWrapperClassName: 'overflow-x-auto min-h-full!',
          tableClassName: 'font-inter w-full table-auto min-h-full!',
          headerRowClassName: 'border-b border-b-gray-200',
          headerColumnClassName:
            'px-6 py-3 text-xs font-semibold text-gray-500 last:flex last:flex-row last:justify-end',
          bodyRowClassName: 'border-b border-b-gray-200',
          bodyColumnClassName:
            'px-6 py-3 last:flex last:flex-row last:justify-end',
        }}
      />
    </Card>
  );
};

export default StockProductWarehouseTable;
