import { createElement, lazy } from 'react'

const ProductStockInfo = lazy(
  () => import('../../components/Dashboard/Inventory/ProductStocks/ProductStockInfo')
)
const PostProductStock = lazy(
  () => import('../../components/Dashboard/Inventory/ProductStocks/PostProductStock')
)
import ProductStockIcon from '../../components/Icons/ProductStockIcon'
import InfoCircleIcon from '../../components/Icons/InfoCircleIcon'
import EditIcon from '../../components/Icons/EditIcon'
import CheckIcon from '../../components/Icons/CheckIcon'
import XMarkIcon from '../../components/Icons/XMarkIcon'
import BinIcon from '../../components/Icons/BinIcon'

export const ProductStock = {
  name: 'productStock',
  label: 'Product Stock',
  labelPlural: 'Product Stocks',
  url: '/dashboard/inventory/productstocks',
  prefix: 'PDS',
  icon: ProductStockIcon,
  actions: [
    {
      name: 'info',
      type: 'page',
      pageName: 'info',
      label: 'Info',
      default: true,
      row: true,
      icon: InfoCircleIcon
    },
    {
      name: 'edit',
      type: 'page',
      pageName: 'info',
      label: 'Edit',
      row: true,
      icon: EditIcon,
      visible: (objectData) => {
        return !(objectData?._isEditing && objectData?._isEditing == true)
      },
      disabled: (objectData) => {
        return objectData?.state?.type != 'draft'
      }
    },
    {
      name: 'cancelEdit',
      label: 'Cancel Edits',
      type: 'page',
      pageName: 'info',
      icon: XMarkIcon,
      visible: (objectData) => {
        return objectData?._isEditing && objectData?._isEditing == true
      }
    },
    {
      name: 'finishEdit',
      label: 'Save Edits',
      type: 'page',
      pageName: 'info',
      icon: CheckIcon,
      visible: (objectData) => {
        return objectData?._isEditing && objectData?._isEditing == true
      }
    },
    {
      name: 'delete',
      type: 'page',
      pageName: 'info',
      label: 'Delete',
      icon: BinIcon,
      danger: true,
      visible: (objectData) => {
        return !(objectData?._isEditing && objectData?._isEditing == true)
      },
      disabled: (objectData) => {
        return objectData?.state?.type != 'draft'
      }
    },
    { type: 'divider' },
    {
      name: 'post',
      type: 'modal',
      modalWidth: 520,
      label: 'Post',
      icon: CheckIcon,
      visible: (objectData) => {
        return objectData?.state?.type == 'draft'
      },
      content: (objectData, { onOk } = {}) => {
        return createElement(PostProductStock, { objectData, onOk })
      }
    }
  ],
  pages: [
    {
      name: 'info',
      content: () => createElement(ProductStockInfo)
    }
  ],
  filters: [
    'productSku',
    'state',
    'currentQuantity',
    'productSku._id',
    'stockLocation',
    'stockLocation._id',
    'createdAt',
    'updatedAt',
    '_reference'
  ],
  sorters: [
    'productSku',
    'currentQuantity',
    'state',
    'createdAt',
    'updatedAt'
  ],
  columns: [
    '_reference',
    'state',
    'currentQuantity',
    'productSku',
    'stockLocation',
    'createdAt',
    'updatedAt'
  ],
  properties: [
    {
      name: '_id',
      label: 'ID',
      type: 'id',
      objectType: 'productStock',
      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: 'productStock',
      showCopy: true,
      readOnly: true,
      columnWidth: 180
    },
    {
      name: 'state',
      label: 'State',
      type: 'state',
      readOnly: true,
      columnWidth: 260
    },
    {
      name: 'postedAt',
      label: 'Posted At',
      type: 'dateTime',
      readOnly: true,
      columnWidth: 175
    },
    {
      name: 'updatedAt',
      label: 'Updated At',
      type: 'dateTime',
      readOnly: true,
      columnWidth: 175
    },
    {
      name: 'productSku',
      label: 'Product SKU',
      type: 'object',
      objectType: 'productSku',
      required: true,
      showHyperlink: true,
      columnWidth: 200
    },
    {
      name: 'stockLocation',
      label: 'Stock location',
      type: 'object',
      objectType: 'stockLocation',
      required: false,
      showHyperlink: true,
      columnWidth: 200,
      readOnly: (objectData) => {
        return objectData?.state?.type != 'draft'
      }
    },
    {
      name: 'currentQuantity',
      label: 'Current Quantity',
      type: 'number',
      columnWidth: 200,
      required: true
    },
    {
      name: 'partStocks',
      label: 'Part Stocks',
      type: 'objectChildren',
      canAddRemove: false,
      properties: [
        {
          name: 'partSku',
          label: 'Part SKU',
          type: 'object',
          objectType: 'partSku',
          readOnly: true,
          required: true,
          showHyperlink: true
        },
        {
          name: 'partStock',
          label: 'Part Stock',
          type: 'object',
          objectType: 'partStock',
          required: true,
          showHyperlink: true,
          masterFilter: (objectData) => {
            const partSkuId = objectData?.partSku?._id
            if (partSkuId == null) return {}
            return { 'partSku._id': partSkuId }
          }
        },
        {
          name: 'quantity',
          label: 'Quantity',
          type: 'number',
          required: true
        }
      ]
    }
  ],
  stats: [
    {
      name: 'draft.count',
      label: 'Draft',
      type: 'number',
      color: 'default'
    },
    {
      name: 'posted.count',
      label: 'Posted',
      type: 'number',
      color: 'success'
    },
    {
      name: 'totalCurrentQuantity.sum',
      label: 'Total Current Quantity',
      type: 'number',
      roundNumber: 2,
      cardWidth: 200
    }
  ]
}
