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 { 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 { TableListViewConfig } from '@dvn/core/models/table-list-view-config';
import { AppViewType } from '@dvn/core/models/view-config';
import { dataConfigs } from '../data-configs';
import { spaces } from '../spaces';
import * as detailViewUserAuth from './detail-view-user-auth';
export const ID = 'table-list-view-user';
const listViewUIConfig = TableListViewConfig.decode({
  header: {
    actions: [],
  },
  main: {
    actions: [
      {
        ui: {
          name: 'expand',
          buttonType: 'icon-button',
          iconName: 'uil uil-key-skeleton',
          view: 'flat'
        },
        data: {
          type: 'navigation',
          direction: 'forward',
          placement: ViewPlacement.SideModal,
          viewId: detailViewUserAuth.ID,
          params: {
            conversionConfig: {
              'item._id': 'userId',
            }
          }
        }
      },
    ],
    fields: [
      { fieldId: 'imageUrl' },
      { fieldId: 'firstName' },
      { fieldId: 'lastName' },
      { fieldId: 'email' },
      { 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' },
    ],
    columns: {
      createdAt: {
        width: 120,
      },
      email: {
        width: 240,
      },
      firstName: {
        width: 150,
      },
      imageUrl: {
        width: 50,
      },
      lastName: {
        width: 150,
      },
      'lastSession.updatedAt': {
        width: 150,
      },
      status: {
        width: 100,
      },
      memberships: {
        weight: 1,
      },
    },
  },
  quickSearch: {
    fields: [
      { fieldId: 'email' },
      { fieldId: 'firstName' },
      { fieldId: 'lastName' },
      { fieldId: 'status' },
    ]
  },
});
export const viewConfig = TableListViewConfig.decode({
  _id: ID,
  type: AppViewType.ListViewTable,
  base: 'mentornity',
  title: 'Users',
  space: spaces['mentornity'],
  table: dataConfigs['users'],
  query: {
    fields: [
      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 TextField('status', '')
      ], [
        new TextField('logoUrl', ''),
        new TextField('title', ''),
      ]),
      {
        type: 'reverse-reference',
        id: 'lastSession',
        otherCollection: 'sessions',
        nameInOtherCollection: 'user',
        isSingle: true,
        fields: [{ id: 'updatedAt' }, { id: 'createdAt' }],
        limit: 1,
        sort: {
          order: -1,
          id: 'updatedAt'
        }
      }
    ],
    sort: [{ id: 'createdAt', order: -1 }]
  },
  ui: listViewUIConfig,
});
