import { createElement, lazy } from 'react'

const CourierServiceInfo = lazy(
  () => import('../../components/Dashboard/Management/CourierServices/CourierServiceInfo')
)
import CourierServiceIcon from '../../components/Icons/CourierServiceIcon'
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 CourierService = {
  name: 'courierService',
  label: 'Courier Service',
  labelPlural: 'Courier Services',
  url: '/dashboard/management/courierservices',
  prefix: 'COS',
  icon: CourierServiceIcon,
  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(CourierServiceInfo)
    }
  ],
  columns: [
    '_reference',
    'name',
    'courier',
    'tracked',
    'deliveryTime',
    'cost',
    'costWithTax',
    'active',
    'createdAt',
    'updatedAt'
  ],
  filters: [
    '_id',
    'courier',
    'courier._id',
    'name',
    'active',
    'tracked',
    'deliveryTime',
    'cost',
    'costWithTax',
    'createdAt',
    'updatedAt',
    '_reference'
  ],
  sorters: [
    'name',
    'courier',
    'active',
    'tracked',
    'cost',
    'costWithTax',
    'estimatedDeliveryTime',
    'createdAt',
    '_id',
    'updatedAt'
  ],
  group: ['courier'],
  properties: [
    {
      name: '_id',
      label: 'ID',
      columnFixed: 'left',
      type: 'id',
      objectType: 'courierService',
      showCopy: true,
      columnWidth: 140
    },
    {
      name: 'createdAt',
      label: 'Created At',
      type: 'dateTime',
      readOnly: true,
      columnWidth: 175
    },
    {
      name: '_reference',
      label: 'Reference',
      type: 'reference',
      columnFixed: 'left',
      objectType: 'courierService',
      showCopy: true,
      readOnly: true,
      columnWidth: 180
    },
    {
      name: 'name',
      label: 'Name',
      columnFixed: 'left',
      required: true,
      type: 'text',
      columnWidth: 200
    },
    {
      name: 'updatedAt',
      label: 'Updated At',
      type: 'dateTime',
      readOnly: true,
      columnWidth: 175
    },
    {
      name: 'courier',
      label: 'Courier',
      type: 'object',
      objectType: 'courier',
      showHyperlink: true,
      required: true,
      columnWidth: 200
    },
    {
      name: 'deliveryTime',
      label: 'Delivery Time',
      type: 'number',
      readOnly: false,
      required: true,
      suffix: 'days',
      columnWidth: 175
    },
    {
      name: 'active',
      label: 'Active',
      type: 'bool',
      readOnly: false,
      required: true,
      columnWidth: 125
    },
    {
      name: 'cost',
      label: 'Cost',
      type: 'number',
      prefix: '£',
      min: 0,
      step: 0.01,
      readOnly: false,
      required: true,
      columnWidth: 150
    },
    {
      name: 'costWithTax',
      label: 'Cost w/ Tax',
      type: 'number',
      prefix: '£',
      min: 0,
      step: 0.01,
      readOnly: true,
      required: true,
      columnWidth: 150,
      value: (objectData) => {
        const cost = objectData?.cost
        if (cost == null) return 0
        if (objectData?.costTaxRate?.rateType == 'percentage') {
          return (
            (cost * (1 + objectData.costTaxRate.rate / 100)).toFixed(2) || 0
          )
        } else if (objectData?.costTaxRate?.rateType == 'amount') {
          return (cost + objectData.costTaxRate.rate).toFixed(2) || 0
        }
        return cost
      }
    },
    {
      name: 'costTaxRate',
      label: 'Cost Tax Rate',
      required: true,
      type: 'object',
      objectType: 'taxRate',
      showHyperlink: true,
      columnWidth: 150
    },
    {
      name: 'additionalCost',
      label: 'Additional Item Cost',
      type: 'number',
      prefix: '£',
      min: 0,
      step: 0.01,
      readOnly: false,
      required: false,
      columnWidth: 220
    },
    {
      name: 'additionalCostWithTax',
      label: 'Additional Item Cost w/ Tax',
      type: 'number',
      prefix: '£',
      min: 0,
      step: 0.01,
      readOnly: true,
      required: false,
      columnWidth: 220,
      value: (objectData) => {
        const cost = objectData?.additionalCost
        if (cost == null) return undefined
        if (objectData?.costTaxRate?.rateType == 'percentage') {
          return (
            (cost * (1 + objectData.costTaxRate.rate / 100)).toFixed(2) || 0
          )
        } else if (objectData?.costTaxRate?.rateType == 'amount') {
          return (cost + objectData.costTaxRate.rate).toFixed(2) || 0
        }
        return cost
      }
    },
    {
      name: 'shippingCurrency',
      label: 'Shipping Currency',
      type: 'text',
      readOnly: false,
      required: true,
      columnWidth: 160
    },
    {
      name: 'international',
      label: 'International Service',
      type: 'bool',
      readOnly: false,
      required: true,
      columnWidth: 170
    },
    {
      name: 'tracked',
      label: 'Tracked',
      type: 'bool',
      readOnly: false,
      required: true,
      columnWidth: 150
    },
    {
      name: 'website',
      label: 'Website',
      columnWidth: 300,
      type: 'url',
      readOnly: false,
      required: false
    }
  ]
}
