import { createElement, lazy } from 'react'

const ProductInfo = lazy(
  () => import('../../components/Dashboard/Management/Products/ProductInfo')
)
const NewProductSku = lazy(
  () => import('../../components/Dashboard/Management/ProductSkus/NewProductSku')
)
import ProductIcon from '../../components/Icons/ProductIcon'
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 PlusIcon from '../../components/Icons/PlusIcon'

export const Product = {
  name: 'product',
  label: 'Product',
  labelPlural: 'Products',
  url: '/dashboard/management/products',
  prefix: 'PRD',
  icon: ProductIcon,
  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: 'newProductSku',
      type: 'modal',
      modalWidth: 700,
      label: 'New Product SKU',
      icon: PlusIcon,
      visible: (objectData) => {
        return !(objectData?._isEditing && objectData?._isEditing == true)
      },
      content: (objectData, { onOk } = {}) => {
        return createElement(NewProductSku, { defaultValues: { product: objectData }, onOk, reset: true })
      }
    }
  ],
  pages: [
    {
      name: 'info',
      content: () => createElement(ProductInfo)
    }
  ],
  columns: [
    '_reference',
    'name',
    'productCategory',
    'tags',
    'vendor',
    'cost',
    'costWithTax',
    'price',
    'priceWithTax',
    'createdAt',
    'updatedAt'
  ],
  filters: [
    '_id',
    'name',
    'globalPrice',
    'productCategory',
    'productCategory._id',
    'createdAt',
    'updatedAt',
    '_reference'
  ],
  sorters: [
    'name',
    'productCategory',
    'createdAt',
    'type',
    'vendor',
    'cost',
    'costWithTax',
    'price',
    'priceWithTax',
    'updatedAt'
  ],
  properties: [
    {
      name: '_id',
      label: 'ID',
      type: 'id',
      objectType: 'product',
      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: 'product',
      showCopy: true,
      readOnly: true,
      columnWidth: 180
    },
    {
      name: 'updatedAt',
      label: 'Updated At',
      type: 'dateTime',
      readOnly: true,
      columnWidth: 175
    },
    {
      name: 'name',
      label: 'Name',
      required: true,
      type: 'text',
      columnWidth: 200,
      columnFixed: 'left'
    },
    {
      name: 'productCategory',
      label: 'Category',
      required: true,
      type: 'object',
      objectType: 'productCategory',
      showHyperlink: true,
      columnWidth: 200
    },
    {
      name: 'version',
      label: 'Version',
      required: false,
      type: 'text',
      columnWidth: 120
    },
    {
      name: 'vendor',
      label: 'Vendor',
      required: true,
      type: 'object',
      objectType: 'vendor',
      showHyperlink: true,
      columnWidth: 200
    },
    {
      name: 'tags',
      label: 'Tags',
      required: false,
      type: 'tags',
      columnWidth: 200
    },
    {
      name: 'cost',
      label: 'Cost',
      required: true,
      type: 'number',
      prefix: '£',
      min: 0,
      step: 0.01,
      columnWidth: 100
    },
    {
      name: 'costWithTax',
      label: 'Cost w/ Tax',
      required: true,
      readOnly: true,
      type: 'number',
      prefix: '£',
      min: 0,
      step: 0.01,
      columnWidth: 150,
      value: (objectData) => {
        const cost = objectData?.cost
        if (!cost) 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: 'priceMode',
      label: 'Price Mode',
      required: true,
      type: 'priceMode',
      columnWidth: 150
    },
    {
      name: 'price',
      label: 'Price',
      required: true,
      type: 'number',
      prefix: '£',
      min: 0,
      step: 0.1,
      columnWidth: 100,
      readOnly: (objectData) => objectData?.priceMode == 'margin',
      value: (objectData) => {
        if (
          objectData?.priceMode == 'margin' &&
          objectData?.margin !== undefined &&
          objectData?.margin !== null &&
          objectData?.cost != null
        ) {
          return (
            (objectData.cost * (1 + objectData.margin / 100)).toFixed(2) || 0
          )
        }
        return objectData?.price
      }
    },
    {
      name: 'margin',
      label: 'Margin',
      required: true,
      type: 'number',
      readOnly: (objectData) => objectData?.priceMode == 'amount',
      suffix: '%',
      min: 0,
      max: 100,
      step: 0.01,
      value: (objectData) => {
        const priceMode = objectData?.priceMode
        const cost = objectData?.cost
        if (priceMode == 'amount') {
          const price = objectData?.price
          if (price != null && cost != null) {
            return Number(((price / cost - 1) * 100).toFixed(2)) || undefined
          }
          return undefined
        }
        return objectData?.margin
      },
      columnWidth: 85
    },
    {
      name: 'priceWithTax',
      label: 'Price w/ Tax',
      required: true,
      readOnly: true,
      type: 'number',
      prefix: '£',
      min: 0,
      step: 0.01,
      columnWidth: 150,
      value: (objectData) => {
        let price
        if (
          objectData?.priceMode == 'margin' &&
          objectData?.margin != null &&
          objectData?.cost != null
        ) {
          price = objectData.cost * (1 + objectData.margin / 100)
        } else {
          price = objectData?.price
        }
        if (!price) return 0
        if (objectData?.priceTaxRate?.rateType == 'percentage') {
          return (
            (price * (1 + objectData?.priceTaxRate?.rate / 100)).toFixed(2) || 0
          )
        } else if (objectData?.priceTaxRate?.rateType == 'amount') {
          return (price + objectData?.priceTaxRate?.rate).toFixed(2) || 0
        }
        return price
      }
    },
    {
      name: 'priceTaxRate',
      label: 'Price Tax Rate',
      required: true,
      type: 'object',
      objectType: 'taxRate',
      showHyperlink: true,
      columnWidth: 150
    }
  ]
}
