import { createElement, lazy } from 'react'

const UserInfo = lazy(
  () => import('../../components/Dashboard/Management/Users/UserInfo')
)
const NewAppPassword = lazy(
  () =>
    import('../../components/Dashboard/Management/AppPasswords/NewAppPassword')
)
import PersonIcon from '../../components/Icons/PersonIcon'
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 User = {
  name: 'user',
  label: 'User',
  labelPlural: 'Users',
  url: '/dashboard/management/users',
  prefix: 'USR',
  icon: PersonIcon,
  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
      }
    },
    {
      name: 'newAppPassword',
      type: 'modal',
      modalWidth: 680,
      label: 'New App Password',
      icon: PlusIcon,
      disabled: (objectData) => {
        return objectData?._user?._id != objectData?._id
      },
      content: (objectData, { onOk } = {}) => {
        console.log('newAppPassword objectData', objectData)
        return createElement(NewAppPassword, {
          defaultValues: { user: objectData._user },
          onOk,
          reset: true
        })
      }
    }
  ],
  pages: [
    {
      name: 'info',
      content: () => createElement(UserInfo)
    }
  ],
  columns: ['_reference', 'name', 'username', 'email', 'role', 'createdAt', 'updatedAt'],
  filters: [
    'name',
    'username',
    'firstName',
    'lastName',
    'email',
    'createdAt',
    'updatedAt',
    '_reference'
  ],
  sorters: ['name', 'email', 'role', 'createdAt', '_id', 'updatedAt'],
  properties: [
    {
      name: '_id',
      label: 'ID',
      columnFixed: 'left',
      type: 'id',
      objectType: 'user',
      showCopy: true,
      columnWidth: 140
    },
    {
      name: 'createdAt',
      label: 'Created At',
      type: 'dateTime',
      readOnly: true,
      columnWidth: 175
    },
    {
      name: '_reference',
      label: 'Reference',
      type: 'reference',
      columnFixed: 'left',
      objectType: 'user',
      showCopy: true,
      readOnly: true,
      columnWidth: 180
    },
    {
      name: 'updatedAt',
      label: 'Updated At',
      type: 'dateTime',
      readOnly: true,
      columnWidth: 175
    },
    {
      name: 'name',
      label: 'Name',
      columnFixed: 'left',
      required: true,
      type: 'text',
      columnWidth: 200
    },

    {
      name: 'username',
      label: 'Username',
      required: true,
      type: 'text',
      columnWidth: 150
    },

    {
      name: 'firstName',
      label: 'First Name',
      type: 'text',
      columnWidth: 130
    },

    {
      name: 'lastName',
      label: 'Last Name',
      type: 'text',
      columnWidth: 130
    },
    {
      name: 'email',
      label: 'Email',
      columnWidth: 300,
      type: 'email'
    },
    {
      name: 'profileImage',
      label: 'Profile Image',
      type: 'file',
      fileType: 'image',
      previewOpen: true,
      showPreview: false,
      thumbnail: true,
      columnWidth: 150,
      masterFilter: ['.png', '.jpg', '.jpeg']
    }
  ]
}
