import { createElement, lazy } from 'react'

const SubJobInfo = lazy(
  () => import('../../components/Dashboard/Production/SubJobs/SubJobInfo')
)
import SubJobIcon from '../../components/Icons/SubJobIcon'
import InfoCircleIcon from '../../components/Icons/InfoCircleIcon'
import XMarkIcon from '../../components/Icons/XMarkIcon'
import dayjs from 'dayjs'

export const SubJob = {
  name: 'subJob',
  label: 'Sub Job',
  labelPlural: 'Sub Jobs',
  url: '/dashboard/production/subjobs',
  prefix: 'SJB',
  icon: SubJobIcon,
  actions: [
    {
      name: 'info',
      type: 'page',
      pageName: 'info',
      label: 'Info',
      default: true,
      row: true,
      icon: InfoCircleIcon
    },
    {
      name: 'cancel',
      type: 'page',
      pageName: 'info',
      label: 'Cancel Sub Job',
      row: true,
      icon: XMarkIcon,
      disabled: (objectData) => {
        return objectData?.state?.type !== 'queued'
      }
    }
  ],
  pages: [
    {
      name: 'info',
      content: () => createElement(SubJobInfo)
    }
  ],
  columns: [
    '_reference',
    'printer',
    'job',
    'state',
    'startedAt',
    'finishedAt',
    'createdAt',
    'updatedAt'
  ],
  filters: [
    '_id',
    'job',
    'printer',
    '_reference',
    'state',
    'createdAt',
    'updatedAt',
    'startedAt',
    'finishedAt'
  ],
  sorters: [
    'createdAt',
    'state',
    'updatedAt',
    'startedAt',
    'finishedAt',
    'job',
    'printer'
  ],
  group: ['job'],
  properties: [
    {
      name: '_id',
      label: 'ID',
      type: 'id',
      columnFixed: 'left',
      objectType: 'subJob',
      columnWidth: 140,
      showCopy: true
    },
    {
      name: 'createdAt',
      label: 'Created At',
      type: 'dateTime',
      readOnly: true,
      columnWidth: 175
    },
    {
      name: '_reference',
      label: 'Reference',
      type: 'reference',
      columnFixed: 'left',
      objectType: 'subJob',
      showCopy: true,
      readOnly: true,
      columnWidth: 180
    },
    {
      name: 'updatedAt',
      label: 'Updated At',
      type: 'dateTime',
      readOnly: true,
      columnWidth: 175
    },
    {
      name: 'state',
      label: 'State',
      type: 'state',
      objectType: 'subJob',
      showStatus: true,
      showProgress: true,
      showId: false,
      showQuantity: false,
      columnWidth: 260,
      readOnly: true
    },
    {
      name: 'startedAt',
      label: 'Started At',
      type: 'dateTime',
      readOnly: true,
      columnWidth: 175
    },

    {
      name: 'moonrakerJobId',
      label: 'Moonraker Job ID',
      type: 'miscId',
      columnWidth: 140,
      showCopy: true
    },

    {
      name: 'finishedAt',
      label: 'Finished At',
      type: 'dateTime',
      readOnly: true,
      columnWidth: 175
    },

    {
      name: 'job',
      label: 'Job',
      type: 'object',
      objectType: 'job',
      showHyperlink: true,
      columnWidth: 200
    },

    {
      name: 'printer',
      label: 'Printer',
      type: 'object',
      columnFixed: 'left',
      objectType: 'printer',
      showHyperlink: true,
      columnWidth: 200
    },
    {
      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(' ')
      }
    }
  ]
}
