import { ImagePosition } from '@dvn/alaca/components/dvn-card-header/models';
import { AttachmentField } from '@dvn/core/doldurma/src/fields/attachment';
import { DateField } from '@dvn/core/doldurma/src/fields/date';
import { DateDataType } from '@dvn/core/doldurma/src/fields/date/date';
import { NestedField } from '@dvn/core/doldurma/src/fields/nested/nested';
import { DataTransferField } from '@dvn/core/doldurma/src/fields/query-include/data-transfer-field';
import { RelationReferenceField } from '@dvn/core/doldurma/src/fields/relation-reference';
import { TextField } from '@dvn/core/doldurma/src/fields/text';
import { ViewPlacement } from '@dvn/core/models/app-config';
import { DetailViewConfig } from '@dvn/core/models/detail-view-config';
import { Comparison } from '@dvn/core/models/filter';
import { AppViewType } from '@dvn/core/models/view-config';
import { dataConfigs } from '../data-configs';
import { spaces } from '../spaces';
import * as eraseUserFormView from './form-view-user-erase';
export const ID = 'detail-view-user';
export const viewConfig = DetailViewConfig.decode({
  _id: ID,
  base: 'mentornity',
  space: spaces['mentornity'],
  table: dataConfigs['users'],
  title: 'User',
  ui: {
    cardConfig: {
      header: {
        actions: [{
            ui: {
              name: 'erase-user',
              buttonType: 'icon-button',
              iconName: 'uil uil-trash-alt',
              // label: 'Erase User',
              scale: 'medium',
              tint: 'danger',
              view: 'solid',
            },
            data: {
              type: 'navigation',
              viewId: eraseUserFormView.ID,
              direction: 'forward',
              placement: ViewPlacement.Dialog,
              params: {
                conversionConfig: {
                  'item._id': 'userId',
                }
              }
            }
          }],
        cover: {
          // fieldId: 'coverUrl',
          size: 128,
        },
        image: {
          fieldId: 'imageUrl',
          shape: 'circle',
          size: 96,
          position: ImagePosition.Center,
        },
        // title: {
        //   fieldId: 'title',
        // },
      },
      body: {
        fields: [
          {
            fieldId: '_id',
          },
          {
            fieldId: '_roles',
          },
          {
            fieldId: 'email',
          },
          {
            fieldId: 'firstName',
          },
          {
            fieldId: 'lastName',
          },
          {
            fieldId: 'status',
          },
          {
            fieldId: 'createdAt',
          },
          {
            fieldId: 'lastSession.updatedAt',
            fieldDisplay: new NestedField('lastSession.updatedAt', new DateField('updatedAt', 'Last Session')
              .setDataType(DateDataType.Unix)
              .setDateFormat('DD MMM YYYY HH:mm')),
          },
          { fieldId: 'memberships' },
          // {
          //   fieldId: 'memberships',
          // },
          // {
          //   fieldId: 'lastSession',
          // },
        ]
      },
    }
  },
  type: AppViewType.DetailViewCard,
  query: {
    fields: [
      new DataTransferField('_roles'),
      new DateField('createdAt', 'Created At').setDataType(DateDataType.Unix).setDateFormat('DD MMM YYYY'),
      new AttachmentField('imageUrl', ''),
      new TextField('email', 'Email'),
      new TextField('firstName', 'First Name'),
      new TextField('lastName', 'Last Name'),
      new TextField('status', 'Status'),
      new RelationReferenceField('memberships', 'Programs', 'title', 'logoUrl', 'memberships', 'user', 'organisations', 'organisation', [
        new DataTransferField('status'),
        new DataTransferField('updatedBy')
      ], [
        new DataTransferField('logoUrl'),
        new DataTransferField('title'),
      ]),
      {
        type: 'reverse-reference',
        id: 'lastSession',
        otherCollection: 'sessions',
        nameInOtherCollection: 'user',
        isSingle: true,
        fields: [{ id: 'updatedAt' }, { id: 'createdAt' }],
        limit: 1,
        sort: {
          order: -1,
          id: 'updatedAt'
        }
      }
    ],
    filter: {
      comparison: Comparison.Eq,
      id: '_id',
      value: '$itemId',
    },
  },
  params: {
    itemId: {
      required: false,
    },
  },
});
