import { DateField } from '@dvn/core/doldurma/src/fields/date';
import { DateDataType } from '@dvn/core/doldurma/src/fields/date/date';
import { DataTransferField } from '@dvn/core/doldurma/src/fields/query-include/data-transfer-field';
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 detailViewOrganisation from './detail-view-organisation';
import * as formViewOrganisationConfigEdit from './form-view-organisation-config-edit';
import * as formViewOrganisationCreate from './form-view-organisation-create';
import * as tableListViewOrganisationAdmins from './table-list-view-organisation-admins';
export const ID = 'table-list-view-organisation';
export const organisationTableListViewUIConfig = TableListViewConfig.decode({
  header: {
    actions: [{
        ui: {
          name: 'new-organisation',
          buttonType: 'button',
          label: 'Create New'
        },
        data: {
          type: 'navigation',
          direction: 'forward',
          placement: ViewPlacement.Dialog,
          viewId: formViewOrganisationCreate.ID,
        }
      }],
  },
  main: {
    actionsPlacement: 'left',
    actions: [
      {
        ui: {
          name: 'expand',
          buttonType: 'icon-button',
          iconName: 'uil uil-expand-alt',
          view: 'flat'
        },
        data: {
          type: 'navigation',
          direction: 'forward',
          placement: ViewPlacement.SideModal,
          viewId: detailViewOrganisation.ID,
          params: {
            conversionConfig: {
              'item._id': 'itemId',
            }
          }
        }
      },
      {
        ui: {
          name: 'manage-admins',
          buttonType: 'icon-button',
          iconName: 'uil uil-users-alt',
          view: 'flat'
        },
        data: {
          type: 'navigation',
          direction: 'forward',
          placement: ViewPlacement.SideModal,
          viewId: tableListViewOrganisationAdmins.ID,
          params: {
            conversionConfig: {
              'item._id': 'organisationId',
              'item.domain': 'organisationDomain',
            }
          }
        }
      },
      {
        ui: {
          name: 'expand',
          buttonType: 'icon-button',
          iconName: 'uil uil-cog',
          view: 'flat'
        },
        data: {
          type: 'navigation',
          direction: 'forward',
          placement: ViewPlacement.SideModal,
          viewId: formViewOrganisationConfigEdit.ID,
          params: {
            conversionConfig: {
              'item._id': 'itemId',
            }
          }
        }
      },
    ],
    fields: [
      { fieldId: 'logoUrl' },
      { fieldId: 'status', isEditable: true },
      { fieldId: 'model', isEditable: true },
      { fieldId: 'title' },
      { fieldId: 'domain' },
      {
        fieldDisplay: new DateField('createdAt', 'Creation Date')
          .setDateFormat('DD MMM YYYY')
          .setDataType(DateDataType.Unix),
      },
      {
        fieldDisplay: new TextField('summary.metrics.mentorCount', 'Mentors'),
      },
      {
        fieldDisplay: new TextField('summary.metrics.menteeCount', 'Mentees'),
      },
      {
        fieldId: 'config.adminWebAppHomeURL',
        isEditable: true
      },
      {
        fieldId: 'config.adminWebAppHomeMessage',
        isEditable: true
      },
    ],
    columns: {
      title: {
        weight: 1,
        width: 300,
      },
      domain: {
        weight: 1,
        width: 300,
      },
      status: {
        width: 100,
      },
      model: {
        width: 180,
      },
      logoUrl: {
        width: 50,
      },
      createdAt: {
        width: 150,
      },
      'config.adminWebAppHomeURL': {
        width: 240,
      },
      'config.adminWebAppHomeMessage': {
        width: 200,
      },
      'summary.metrics.mentorCount': {
        width: 100,
      },
      'summary.metrics.menteeCount': {
        width: 100,
      },
    },
  },
  quickSearch: {
    fields: [
      { fieldId: 'title' },
      { fieldId: 'domain' },
      { fieldId: 'model' },
      { fieldId: 'status' },
    ],
    fieldUIConfigs: [
      {
        fieldId: 'model',
        width: '200px',
      },
      {
        fieldId: 'title',
        width: '200px',
      },
      {
        fieldId: 'domain',
        width: '200px',
      },
    ]
  },
});
export const viewConfig = TableListViewConfig.decode({
  _id: ID,
  type: AppViewType.ListViewTable,
  base: 'mentornity',
  title: 'Organisations',
  space: spaces['mentornity'],
  table: dataConfigs['organisations'],
  query: {
    fields: [
      new DataTransferField('status'),
      new DataTransferField('model'),
      new DataTransferField('logoUrl'),
      new DataTransferField('domain'),
      new DataTransferField('title'),
      new DataTransferField('description'),
      new DataTransferField('createdAt'),
      {
        type: 'reverse-reference',
        id: 'summary',
        otherCollection: 'organisationSummaries',
        nameInOtherCollection: 'organisation',
        isSingle: true,
        fields: [{ id: 'metrics' }]
      },
      {
        type: 'reverse-reference',
        id: 'config',
        otherCollection: 'configs',
        nameInOtherCollection: 'organisation',
        isSingle: true,
        fields: [{ id: 'adminWebAppHomeURL' }, { id: 'adminWebAppHomeMessage' }]
      },
    ],
    sort: [{ id: 'createdAt', order: -1 }]
  },
  ui: organisationTableListViewUIConfig,
});
