import { DvnDataAdapter } from '@dvn/core/adapters/DvnDataAdapter';
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 { ReferenceField } from '@dvn/core/doldurma/src/fields/reference';
import { SingleChoiceField } from '@dvn/core/doldurma/src/fields/single-choice';
import { TextField } from '@dvn/core/doldurma/src/fields/text';
import { ViewPlacement } from '@dvn/core/models/app-config';
import { Comparison } from '@dvn/core/models/filter';
import { TableListViewConfig } from '@dvn/core/models/table-list-view-config';
import { AppViewType } from '@dvn/core/models/view-config';
import { dataConfigs } from '../data-configs';
import { userDataProvider } from '../data-provider-configs';
import { spaces } from '../spaces';
import * as detailViewErrorLog from './detail-view-error-log';
export const ID = 'table-list-view-error-logs';
export const config = (dataApi) => TableListViewConfig.decode({
  _id: ID,
  type: AppViewType.ListViewTable,
  base: 'mentornity',
  title: 'Error Logs',
  space: spaces['mentornity'],
  table: dataConfigs['errorlogs'],
  query: {
    fields: [
      new DataTransferField('res'),
      new DataTransferField('command'),
      new DataTransferField('status'),
      new DataTransferField('message'),
      new DataTransferField('createdAt'),
      new DataTransferField('status'),
      new ReferenceField('user', 'User', 'firstName', 'imageUrl', 'users', [
        new AttachmentField('imageUrl', ''),
        new TextField('email', 'Email'),
        new TextField('firstName', 'First Name'),
        new TextField('lastName', 'Last Name'),
      ], true),
    ],
    filter: {
      filters: [
        {
          comparison: Comparison.Ne,
          id: 'user._id',
          value: '',
        },
      ],
      operator: 'or'
    },
    sort: [{ id: 'createdAt', order: -1 }]
  },
  ui: TableListViewConfig.decode({
    main: {
      actions: [
        {
          ui: {
            name: 'expand',
            buttonType: 'icon-button',
            iconName: 'uil uil-expand-alt',
            view: 'flat'
          },
          data: {
            type: 'navigation',
            direction: 'forward',
            placement: ViewPlacement.SideModal,
            viewId: detailViewErrorLog.ID,
            params: {
              conversionConfig: {
                'item._id': 'id',
              }
            }
          }
        },
      ],
      fields: [
        {
          fieldId: 'createdAt',
          fieldDisplay: new DateField('createdAt', 'Date').setDateFormat('DD.MM.YYYY HH:mm').setDataType(DateDataType.Unix),
        },
        {
          fieldId: 'user.imageUrl',
          fieldDisplay: new NestedField('user.imageUrl', new AttachmentField('imageUrl', '').setShape('circle'))
        },
        {
          fieldId: 'user.firstName',
          fieldDisplay: new NestedField('user.firstName', new TextField('firstName', 'First Name'))
        },
        {
          fieldId: 'user.lastName',
          fieldDisplay: new NestedField('user.lastName', new TextField('lastName', 'Last Name'))
        },
        {
          fieldId: 'user.email',
          fieldDisplay: new NestedField('user.email', new TextField('email', 'Email'))
        },
        {
          fieldId: 'status',
        },
        {
          fieldId: 'message',
        },
        {
          fieldId: 'res',
        },
      ],
      columns: {
        'user.imageUrl': {
          width: 50,
        },
        'user.firstName': {
          width: 120,
        },
        'user.lastName': {
          width: 120,
        },
        'user.email': {
          width: 220,
        },
        createdAt: {
          width: 150,
        },
        status: {
          width: 80,
        },
        message: {
          weight: 1,
          width: 180,
        },
        res: {
          weight: 1,
          width: 120,
        },
      },
    },
    quickSearch: {
      fields: [
        {
          field: new SingleChoiceField('user._id', 'Search user', undefined, '', 'select').setDataProvider(userDataProvider(dataApi))
            .setDataAdapter(new DvnDataAdapter('firstName+lastName', '_id')),
        },
      ],
      fieldUIConfigs: [
        {
          fieldId: 'user._id',
          width: '240px',
        },
        {
          fieldId: 'status',
          width: '150px',
        },
      ],
    },
  }),
});
