import { createElement, lazy } from 'react'

const FilamentStockInfo = lazy(
  () =>
    import('../../components/Dashboard/Inventory/FilamentStocks/FilamentStockInfo')
)
import FilamentStockIcon from '../../components/Icons/FilamentStockIcon'
import InfoCircleIcon from '../../components/Icons/InfoCircleIcon'

export const FilamentStock = {
  name: 'filamentStock',
  label: 'Filament Stock',
  labelPlural: 'Filament Stocks',
  url: '/dashboard/inventory/filamentstocks',
  prefix: 'FLS',
  readOnly: true,
  icon: FilamentStockIcon,
  actions: [
    {
      name: 'info',
      type: 'page',
      pageName: 'info',
      label: 'Info',
      default: true,
      row: true,
      icon: InfoCircleIcon
    }
  ],
  pages: [
    {
      name: 'info',
      content: () => createElement(FilamentStockInfo)
    }
  ],
  columns: [
    '_reference',
    'state',
    'currentWeight',
    'startingWeight',
    'filament',
    'filamentSku',
    'stockLocation',
    'createdAt',
    'updatedAt'
  ],
  filters: [
    'filament',
    'filament._id',
    'filamentSku',
    'state',
    'startingWeight',
    'currentWeight',
    'filamentSku._id',
    'stockLocation',
    'stockLocation._id',
    'createdAt',
    'updatedAt',
    '_reference'
  ],
  sorters: ['createdAt', 'updatedAt', 'filament', 'filamentSku', 'state'],
  group: ['filament', 'filamentSku'],
  properties: [
    {
      name: '_id',
      label: 'ID',
      type: 'id',
      columnFixed: 'left',
      objectType: 'filamentStock',
      showCopy: true,
      columnWidth: 140
    },
    {
      name: 'createdAt',
      label: 'Created At',
      type: 'dateTime',
      readOnly: true,
      columnWidth: 175
    },
    {
      name: '_reference',
      label: 'Reference',
      type: 'reference',
      columnFixed: 'left',
      objectType: 'filamentStock',
      showCopy: true,
      readOnly: true,
      columnWidth: 180
    },
    {
      name: 'updatedAt',
      label: 'Updated At',
      type: 'dateTime',
      readOnly: true,
      columnWidth: 175
    },
    {
      name: 'state',
      label: 'State',
      type: 'state',
      objectType: 'filamentStock',
      readOnly: true,
      columnWidth: 260
    },
    {
      name: 'filament',
      label: 'Filament',
      type: 'object',
      value: null,
      objectType: 'filament',
      required: true,
      showHyperlink: true,
      columnWidth: 200,
      initial: true
    },
    {
      name: 'filamentSku',
      label: 'Filament SKU',
      type: 'object',
      objectType: 'filamentSku',
      readOnly: true,
      initial: true,
      required: true,
      showHyperlink: true,
      columnWidth: 200,
      masterFilter: (objectData) => {
        return {
          filament: objectData?.filament?._id
        }
      }
    },
    {
      name: 'stockLocation',
      label: 'Stock location',
      type: 'object',
      objectType: 'stockLocation',
      required: true,
      showHyperlink: true,
      columnWidth: 200
    },
    {
      name: 'currentWeight',
      label: 'Current Weight',
      type: 'netGross',
      suffix: 'g',
      readOnly: true,
      required: true,
      columnWidth: 300,
      value: (objectData) => {
        if (objectData?.state?.type === 'unconsumed') {
          return objectData?.startingWeight
        } else {
          return objectData.currentWeight
        }
      }
    },
    {
      name: 'startingWeight',
      label: 'Starting Weight',
      type: 'netGross',
      suffix: 'g',
      readOnly: true,
      initial: true,
      required: true,
      columnWidth: 300,
      difference: (objectData) => {
        return objectData?.filamentSku?.filament?.emptySpoolWeight
      }
    }
  ],
  stats: [
    {
      name: 'totalCurrentWeight.sum',
      label: 'Total Current Weight',
      type: 'number',
      roundNumber: 2,
      suffix: 'g',
      cardWidth: 200
    }
  ]
}
