import { createElement, lazy } from 'react'

const FilamentSkuInfo = lazy(
  () => import('../../components/Dashboard/Management/FilamentSkus/FilamentSkuInfo')
)
import FilamentSkuIcon from '../../components/Icons/FilamentSkuIcon'
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 FilamentSku = {
  name: 'filamentSku',
  label: 'Filament SKU',
  labelPlural: 'Filament SKUs',
  url: '/dashboard/management/filamentskus',
  prefix: 'FSU',
  icon: FilamentSkuIcon,
  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)
      }
    },
    {
      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
      }
    },
    { type: 'divider' },
    {
      name: 'delete',
      type: 'page',
      pageName: 'info',
      label: 'Delete',
      icon: BinIcon,
      danger: true
    }
  ],
  pages: [
    {
      name: 'info',
      content: () => createElement(FilamentSkuInfo)
    }
  ],
  columns: [
    '_reference',
    'name',
    'filament',
    'barcode',
    'color',
    'overrideCost',
    'cost',
    'costWithTax',
    'createdAt',
    'updatedAt'
  ],
  filters: [
    '_id',
    'barcode',
    'filament',
    'filament._id',
    'name',
    'color',
    'cost',
    'costWithTax',
    'createdAt',
    'updatedAt',
    '_reference'
  ],
  sorters: [
    'barcode',
    'filament',
    'name',
    'color',
    'cost',
    'costWithTax',
    'createdAt',
    'updatedAt'
  ],
  group: ['filament'],
  properties: [
    {
      name: '_id',
      label: 'ID',
      type: 'id',
      objectType: 'filamentSku',
      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: 'filamentSku',
      showCopy: true,
      readOnly: true,
      columnWidth: 180
    },
    {
      name: 'name',
      label: 'Name',
      required: true,
      type: 'text',
      columnWidth: 200,
      columnFixed: 'left'
    },
    {
      name: 'updatedAt',
      label: 'Updated At',
      type: 'dateTime',
      readOnly: true,
      columnWidth: 175
    },
    {
      name: 'filament',
      label: 'Filament',
      type: 'object',
      objectType: 'filament',
      required: true,
      showHyperlink: true,
      columnWidth: 200
    },
    {
      name: 'barcode',
      label: 'Barcode',
      required: false,
      type: 'text',
      columnWidth: 150
    },
    {
      name: 'description',
      label: 'Description',
      required: false,
      type: 'text',
      columnWidth: 200
    },
    {
      name: 'color',
      label: 'Color',
      required: true,
      type: 'color',
      columnWidth: 120
    },
    {
      name: 'overrideCost',
      label: 'Override Cost',
      required: true,
      type: 'bool',
      columnWidth: 150
    },
    {
      name: 'cost',
      label: 'Cost',
      required: true,
      type: 'number',
      prefix: '£',
      min: 0,
      step: 0.01,
      visible: (objectData) => {
        return objectData?.overrideCost
      },
      value: (objectData) =>
        objectData?.overrideCost
          ? objectData?.cost
          : objectData?.filament?.cost,
      columnWidth: 100
    },
    {
      name: 'costWithTax',
      label: 'Cost w/ Tax',
      required: true,
      readOnly: true,
      type: 'number',
      prefix: '£',
      min: 0,
      step: 0.01,
      visible: (objectData) => {
        return objectData?.overrideCost
      },
      value: (objectData) => {
        if (!objectData?.overrideCost)
          return objectData?.filament?.costWithTax || undefined
        const cost = objectData?.cost
        const taxRate = objectData?.costTaxRate
        if (!cost) return 0
        if (taxRate?.rateType == 'percentage') {
          return (cost * (1 + taxRate?.rate / 100)).toFixed(2) || 0
        } else if (taxRate?.rateType == 'amount') {
          return (cost + taxRate?.rate).toFixed(2) || 0
        }
        return cost
      },
      columnWidth: 150
    },
    {
      name: 'costTaxRate',
      label: 'Cost Tax Rate',
      required: true,
      type: 'object',
      objectType: 'taxRate',
      showHyperlink: true,
      visible: (objectData) => {
        return objectData?.overrideCost
      },
      value: (objectData) =>
        objectData?.overrideCost
          ? objectData?.costTaxRate
          : objectData?.filament?.costTaxRate,
      columnWidth: 150
    }
  ]
}
