import { createElement, lazy } from 'react'

const JobInfo = lazy(
  () => import('../../components/Dashboard/Production/Jobs/JobInfo')
)
const DeployJob = lazy(
  () => import('../../components/Dashboard/Production/Jobs/DeployJob')
)
import JobIcon from '../../components/Icons/JobIcon'
import InfoCircleIcon from '../../components/Icons/InfoCircleIcon'
import CheckIcon from '../../components/Icons/CheckIcon'
import XMarkIcon from '../../components/Icons/XMarkIcon'
import EditIcon from '../../components/Icons/EditIcon'
import BinIcon from '../../components/Icons/BinIcon'
import dayjs from 'dayjs'

export const Job = {
  name: 'job',
  label: 'Job',
  labelPlural: 'Jobs',
  url: '/dashboard/production/jobs',
  prefix: 'JOB',
  icon: JobIcon,
  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: 'deploy',
      type: 'modal',
      modalWidth: 680,
      label: 'Deploy',
      default: true,
      row: true,
      icon: CheckIcon,
      disabled: (objectData) => {
        return objectData?.state?.type != 'draft'
      },
      content: (objectData, { onOk } = {}) => {
        return createElement(DeployJob, { objectData, onOk })
      }
    }
  ],
  pages: [
    {
      name: 'info',
      content: () => createElement(JobInfo)
    }
  ],
  columns: [
    '_reference',
    'quantity',
    'state',
    'gcodeFile',
    'startedAt',
    'finishedAt',
    'createdAt',
    'updatedAt'
  ],
  filters: [
    'state',
    'gcodeFile',
    'quantity',
    '_id',
    '_reference',
    'createdAt',
    'updatedAt',
    'startedAt',
    'finishedAt'
  ],
  sorters: [
    'createdAt',
    'state',
    'quantity',
    'gcodeFile',
    'updatedAt',
    'startedAt',
    'finishedAt'
  ],
  properties: [
    {
      name: '_id',
      label: 'ID',
      type: 'id',
      columnFixed: 'left',
      objectType: 'job',
      showCopy: true,
      columnWidth: 140
    },
    {
      name: 'createdAt',
      label: 'Created At',
      type: 'dateTime',
      readOnly: true,
      columnWidth: 175
    },
    {
      name: '_reference',
      label: 'Reference',
      type: 'reference',
      columnFixed: 'left',
      objectType: 'job',
      showCopy: true,
      readOnly: true,
      columnWidth: 180
    },
    {
      name: 'updatedAt',
      label: 'Updated At',
      type: 'dateTime',
      readOnly: true,
      columnWidth: 175
    },
    {
      name: 'state',
      label: 'State',
      type: 'state',
      objectType: 'job',
      showStatus: true,
      showProgress: true,
      showId: false,
      showQuantity: false,
      columnWidth: 260,
      readOnly: true
    },
    {
      name: 'startedAt',
      label: 'Started At',
      type: 'dateTime',
      readOnly: true,
      columnWidth: 175
    },
    {
      name: 'quantity',
      label: 'Quantity',
      type: 'number',
      columnFixed: 'left',
      columnWidth: 125,
      required: true
    },
    {
      name: 'finishedAt',
      label: 'Finished At',
      type: 'dateTime',
      readOnly: true,
      columnWidth: 175
    },

    {
      name: 'gcodeFile',
      label: 'GCode File',
      type: 'object',
      columnWidth: 200,
      objectType: 'gcodeFile',
      required: true,
      showHyperlink: true
    },

    {
      name: 'totalTime',
      label: 'Total Time',
      type: 'text',
      columnWidth: 110,
      readOnly: true,
      value: (objectData) => {
        if (!objectData?.startedAt || !objectData?.finishedAt) {
          return '-'
        }
        const totalSeconds = dayjs(objectData?.finishedAt).diff(
          dayjs(objectData?.startedAt),
          'seconds'
        )
        const days = Math.floor(totalSeconds / 86400)
        const hours = Math.floor((totalSeconds % 86400) / 3600)
        const minutes = Math.floor((totalSeconds % 3600) / 60)
        const seconds = totalSeconds % 60

        const parts = []
        if (days > 0) parts.push(`${days}d`)
        if (hours > 0) parts.push(`${hours}h`)
        if (minutes > 0) parts.push(`${minutes}m`)
        if (seconds > 0 || parts.length === 0) parts.push(`${seconds}s`)

        return parts.join(' ')
      }
    },
    {
      name: 'printers',
      label: 'Printers',
      type: 'objectList',
      objectType: 'printer',
      required: true,
      columnWidth: 200
    }
  ],
  stats: [
    {
      name: 'draft.count',
      label: 'Draft',
      type: 'number',
      color: 'default'
    },
    {
      name: 'queued.count',
      label: 'Queued',
      type: 'number',
      color: 'warning'
    },
    {
      name: 'printing.count',
      label: 'Printing',
      type: 'number',
      color: 'processing'
    },
    {
      name: 'failed.count',
      label: 'Failed',
      type: 'number',
      combine: ['failed.count', 'cancelled.count'],
      color: 'error'
    },
    {
      name: 'complete.count',
      label: 'Complete',
      type: 'number',
      color: 'success'
    }
  ]
}
