import { createElement, lazy } from 'react'

const PartStockInfo = lazy(
  () => import('../../components/Dashboard/Inventory/PartStocks/PartStockInfo')
)
import PartStockIcon from '../../components/Icons/PartStockIcon'
import InfoCircleIcon from '../../components/Icons/InfoCircleIcon'

export const PartStock = {
  name: 'partStock',
  label: 'Part Stock',
  labelPlural: 'Part Stocks',
  url: '/dashboard/inventory/partstocks',
  prefix: 'PTS',
  readOnly: true,
  icon: PartStockIcon,
  actions: [
    {
      name: 'info',
      type: 'page',
      pageName: 'info',
      label: 'Info',
      default: true,
      row: true,
      icon: InfoCircleIcon
    }
  ],
  pages: [
    {
      name: 'info',
      content: () => createElement(PartStockInfo)
    }
  ],
  filters: [
    'partSku',
    'state',
    'startingQuantity',
    'currentQuantity',
    'partSku._id',
    'stockLocation',
    'stockLocation._id',
    'createdAt',
    'updatedAt',
    '_reference'
  ],
  sorters: [
    'partSku',
    'startingQuantity',
    'currentQuantity',
    'state',
    'createdAt',
    'updatedAt'
  ],
  columns: [
    '_reference',
    'state',
    'startingQuantity',
    'currentQuantity',
    'partSku',
    'stockLocation',
    'createdAt',
    'updatedAt'
  ],
  properties: [
    {
      name: '_id',
      label: 'ID',
      type: 'id',
      objectType: 'partStock',
      showCopy: true,
      readOnly: true,
      columnWidth: 140
    },
    {
      name: 'createdAt',
      label: 'Created At',
      type: 'dateTime',
      readOnly: true,
      columnWidth: 175
    },
    {
      name: '_reference',
      label: 'Reference',
      type: 'reference',
      columnFixed: 'left',
      objectType: 'partStock',
      showCopy: true,
      readOnly: true,
      columnWidth: 180
    },
    {
      name: 'state',
      label: 'State',
      type: 'state',
      readOnly: true,
      columnWidth: 260
    },
    {
      name: 'updatedAt',
      label: 'Updated At',
      type: 'dateTime',
      readOnly: true,
      columnWidth: 175
    },
    {
      name: 'sourceType',
      label: 'Source Type',
      type: 'objectType',
      readOnly: false,
      columnWidth: 200,
      required: true,
      masterFilter: ['subJob', 'stockTransfer']
    },
    {
      name: 'partSku',
      label: 'Part SKU',
      type: 'object',
      objectType: 'partSku',
      required: true,
      showHyperlink: true,
      columnWidth: 200
    },
    {
      name: 'stockLocation',
      label: 'Stock location',
      type: 'object',
      objectType: 'stockLocation',
      required: false,
      showHyperlink: true,
      columnWidth: 200
    },

    {
      name: 'source',
      label: 'Source',
      type: 'object',
      readOnly: false,
      required: true,
      columnWidth: 200,
      objectType: (objectData) => {
        return objectData?.sourceType
      },
      showHyperlink: true
    },
    {
      name: 'currentQuantity',
      label: 'Current Quantity',
      type: 'number',
      readOnly: true,
      columnWidth: 200,
      required: true,
      value: (objectData) => {
        if (objectData?.state?.type === 'new') {
          return objectData?.startingQuantity
        } else {
          return objectData.currentQuantity
        }
      }
    }
  ],
  stats: [
    {
      name: 'totalCurrentQuantity.sum',
      label: 'Total Current Quantity',
      type: 'number',
      roundNumber: 2,
      cardWidth: 200
    }
  ]
}
