import { cloneDeep } from 'lodash';

import { Report, reportAccessScope } from './model';
import { ERVU_SCOPES, USER_SCOPES } from '@/constants/entities/Scope';
import { ScopesMode, matchScopes } from '@/utils/scopes';

const columnsBase = {
  create: ['regions', 'organizations', 'accounts', 'comment', 'range', 'every'],
  search: ['regions', 'organizations', 'accounts', 'comment', 'range', 'every'],
  card: [
    'name',
    'services',
    'region',
    'organization',
    'account',
    'startDate',
    'period',
    'comment',
    'download',
  ],
  reports: [
    'id',
    'regions',
    'organizations',
    'startDate',
    'every',
    'comment',
    'download',
  ],
  reportsAuto: [
    'id',
    'regions',
    'organizations',
    'startDate',
    'every',
    'comment',
    'download',
  ],
  attachments: [
    // 'id',
    'date',
    'range',
    'download',
  ],
};

const baseReports = [
  {
    id: '1',
    name: 'Отчет по результатам конвертации',
    scopes: [
      USER_SCOPES.MILITARY_COMMISSAR,
      USER_SCOPES.SYSTEM_ADMINISTRATOR,
      USER_SCOPES.GOMU_SPECIALIST,
      USER_SCOPES.GOMU_OBSERVER,
    ],
    columns: {
      fields: ['created', 'period', 'name', 'download'],
      // ...columnsBase,
      create: [
        {
          name: 'regions',
          props: {
            multiple: false,
            clearable: true,
            hidden: true,
          },
        },
        {
          name: 'organizations',
          props: {
            required: false,
            hidden: true,
          },
        },
        {
          name: 'comment',
          props: {
            required: false,
            hidden: true,
          },
        },
        {
          name: 'rangeWithTime',
          props: {
            required: true,
            hidden: false,
          },
        },
        (() =>
          matchScopesForToggleTestOrganisation([USER_SCOPES.SYSTEM_ADMINISTRATOR]) && {
            name: 'test-organisation',
            props: {
              required: true,
              hidden: false,
            },
          })(),
        // 'time',
        // 'range',
        // 'every',
      ],
      reports: ['id', 'regions', 'organizations', 'comment', 'download'],
      reportsAuto: [
        'id',
        'regions',
        'organizations',

        'last',
        'comment',
        'every',
        'downloadAll',
      ],
    },
    source: 'ervu-person-registry',
    queryDefinitions: {
      'aaa2e2ac-e374-4238-8b80-f4dc81d2f7ba': (
        report: Report,
        role: reportAccessScope
      ) => true,
    },
  },

  {
    id: '2',
    name: 'Сводный отчет по результатам конвертации',
    scopes: [
      USER_SCOPES.SYSTEM_ADMINISTRATOR,
      USER_SCOPES.GOMU_SPECIALIST,
      USER_SCOPES.GOMU_OBSERVER,
    ],
    columns: {
      fields: ['created', 'name', 'download'],
      ...columnsBase,
      create: [
        {
          name: 'regions',
          props: {
            multiple: false,
            clearable: true,
            hidden: true,
          },
        },
        {
          name: 'organizations',
          props: {
            required: false,
            hidden: true,
          },
        },
        {
          name: 'comment',
          props: {
            required: false,
            hidden: true,
          },
        },
        // 'range',
        'every',
      ],
      reports: ['id', 'regions', 'organizations', 'comment', 'download'],
      reportsAuto: [
        'id',
        'regions',
        'organizations',

        // 'last',
        'comment',

        'every',
        'downloadAll',
      ],
    },
    source: 'ervu-person-registry',
    queryDefinitions: {
      '3c03d33c-7290-400d-b2f5-3396c9e67335': (
        report: Report,
        role: reportAccessScope
      ) => true,
    },
  },
  // {
  //   id: '11',
  //   name: 'Сводный отчет по первоначальной постановке на воинский учет граждан мужского пола 17 лет',
  //   scopes: [
  //     USER_SCOPES.SYSTEM_ADMINISTRATOR,
  //     USER_SCOPES.GOMU_SPECIALIST,
  //     USER_SCOPES.GOMU_OBSERVER,
  //     USER_SCOPES.MILITARY_COMMISSAR,
  //     USER_SCOPES.MILITARY_RECRUIT_SPECIALIST,
  //   ],
  //   columns: {
  //     fields: ['created', 'year', 'name', 'download'],
  //     ...columnsBase,
  //     create: [
  //       {
  //         name: 'regions',
  //         props: {
  //           multiple: false,
  //           clearable: true,
  //           hidden: true,
  //         },
  //       },
  //       {
  //         name: 'organizations',
  //         props: {
  //           required: false,
  //           hidden: true,
  //         },
  //       },
  //       {
  //         name: 'comment',
  //         props: {
  //           required: false,
  //           hidden: true,
  //         },
  //       },
  //     (() =>
  //         matchScopesForToggleTestOrganisation([USER_SCOPES.SYSTEM_ADMINISTRATOR]) && {
  //           name: 'test-organisation',
  //           props: {
  //             required: true,
  //             hidden: false,
  //           },
  //         })(),
  //       'year',
  //       // 'range',
  //       // 'every',
  //     ],
  //     reports: ['id', 'regions', 'organizations', 'comment', 'download'],
  //     reportsAuto: [
  //       'id',
  //       'regions',
  //       'organizations',
  //
  //       // 'last',
  //       'comment',
  //
  //       'every',
  //       'downloadAll',
  //     ],
  //   },
  //   source: 'ervu-person-registry',
  //   queryDefinitions: {
  //     '4bef46df-3608-4da7-a317-32b5843f6990': (
  //       report: Report,
  //       role: reportAccessScope
  //     ) => true,
  //   },
  // },
  // {
  //   id: '3',
  //   name: 'Журнал загрузки файлов',
  //   scopes: [
  //     USER_SCOPES.SYSTEM_ADMINISTRATOR,
  //     USER_SCOPES.GOMU_SPECIALIST,
  //     USER_SCOPES.GOMU_OBSERVER,
  //     USER_SCOPES.MILITARY_COMMISSAR,
  //     USER_SCOPES.MILITARY_RECRUIT_SPECIALIST,
  //   ],
  //   columns: {
  //     fields: ['created', 'period', 'name', 'download'],
  //     ...columnsBase,
  //     create: [
  //       {
  //         name: 'regions',
  //         props: {
  //           multiple: false,
  //           clearable: true,
  //           hidden: true,
  //         },
  //       },
  //       {
  //         name: 'organizations',
  //         props: {
  //           required: false,
  //           hidden: true,
  //         },
  //       },
  //       {
  //         name: 'comment',
  //         props: {
  //           required: false,
  //           hidden: true,
  //         },
  //       },
  //       'range',
  //       'every',
  //     ],
  //     reports: ['id', 'regions', 'organizations', 'comment', 'download'],
  //     reportsAuto: [
  //       'id',
  //       'regions',
  //       'organizations',
  //
  //       // 'last',
  //       'comment',
  //
  //       'every',
  //       'downloadAll',
  //     ],
  //   },
  //   source: 'ervu-journal-service',
  //   queryDefinitions: {
  //     '9e295fd7-4f5b-41e9-adee-281ed000648f': (
  //       report: Report,
  //       role: reportAccessScope
  //     ) => true,
  //   },
  // },
  {
    id: '4',
    name: 'Отчет по повесткам',
    scopes: [
      USER_SCOPES.SYSTEM_ADMINISTRATOR,
      USER_SCOPES.GOMU_SPECIALIST,
      USER_SCOPES.GOMU_OBSERVER,
      USER_SCOPES.MILITARY_COMMISSAR,
      USER_SCOPES.MILITARY_RECRUIT_SPECIALIST,
    ],
    columns: {
      fields: ['created', 'period', 'name', 'download'],
      ...columnsBase,
      create: [
        {
          name: 'regions',
          props: {
            multiple: false,
            clearable: true,
            hidden: true,
          },
        },
        {
          name: 'organizations',
          props: {
            required: false,
            hidden: true,
          },
        },
        {
          name: 'comment',
          props: {
            required: false,
            hidden: true,
          },
        },
      (() =>
          matchScopesForToggleTestOrganisation([USER_SCOPES.SYSTEM_ADMINISTRATOR]) && {
            name: 'test-organisation',
            props: {
              required: true,
              hidden: false,
            },
          })(),
        'range',
        'every',
      ],
      reports: ['id', 'regions', 'organizations', 'comment', 'download'],
      reportsAuto: [
        'id',
        'regions',
        'organizations',

        // 'last',
        'comment',

        'every',
        'downloadAll',
      ],
    },
    source: 'ervu-subpoena-registry',
    queryDefinitions: {
      'ac8b8c6d-a657-462b-92da-c9c7b146405f': (
        report: Report,
        role: reportAccessScope
      ) => true,
    },
  },
  {
    id: '5',
    name: 'Сводный отчет по постановке на воинский учет',
    scopes: [
      USER_SCOPES.SYSTEM_ADMINISTRATOR,
      USER_SCOPES.GOMU_SPECIALIST,
      USER_SCOPES.GOMU_OBSERVER,
      USER_SCOPES.MILITARY_COMMISSAR,
      USER_SCOPES.MILITARY_RECRUIT_SPECIALIST,
    ],
    columns: {
      fields: ['created', 'period', 'name', 'download'],
      ...columnsBase,
      create: [
        {
          name: 'regions',
          props: {
            multiple: false,
            clearable: true,
            hidden: true,
          },
        },
        {
          name: 'organizations',
          props: {
            required: false,
            hidden: true,
          },
        },
        {
          name: 'comment',
          props: {
            required: false,
            hidden: true,
          },
        },
        (() =>
          matchScopesForToggleTestOrganisation([USER_SCOPES.SYSTEM_ADMINISTRATOR]) && {
            name: 'test-organisation',
            props: {
              required: true,
              hidden: false,
            },
          })(),
        'range',
        'every',
      ],
      reports: ['id', 'regions', 'organizations', 'comment', 'download'],
      reportsAuto: [
        'id',
        'regions',
        'organizations',
        'comment',
        'every',
        'downloadAll',
      ],
    },
    source: 'ervu-person-registry',
    queryDefinitions: {
      '6fa281ff-bf19-4247-9e1a-2ed441604bf7': (
        report: Report,
        role: reportAccessScope
      ) => true,
    },
  },
  {
    id: '6',
    name: 'Сводный отчет по применению временных мер',
    scopes: [
      USER_SCOPES.SYSTEM_ADMINISTRATOR,
      USER_SCOPES.GOMU_SPECIALIST,
      USER_SCOPES.GOMU_OBSERVER,
      USER_SCOPES.MILITARY_COMMISSAR,
      USER_SCOPES.MILITARY_RECRUIT_SPECIALIST,
    ],
    columns: {
      fields: ['created', 'period', 'name', 'download'],
      ...columnsBase,
      create: [
        {
          name: 'regions',
          props: {
            multiple: false,
            clearable: true,
            hidden: true,
          },
        },
        {
          name: 'organizations',
          props: {
            required: false,
            hidden: true,
          },
        },
        {
          name: 'comment',
          props: {
            required: false,
            hidden: true,
          },
        },
        (() =>
          matchScopesForToggleTestOrganisation([USER_SCOPES.SYSTEM_ADMINISTRATOR]) && {
            name: 'test-organisation',
            props: {
              required: true,
              hidden: false,
            },
          })(),
        'range',
        'every',
      ],
      reports: ['id', 'regions', 'organizations', 'comment', 'download'],
      reportsAuto: [
        'id',
        'regions',
        'organizations',
        'comment',
        'every',
        'downloadAll',
      ],
    },
    source: 'ervu-subpoena-registry',
    queryDefinitions: {
      'f4284825-4bf1-4daa-a430-b9ea78253722': (
        report: Report,
        role: reportAccessScope
      ) => true,
    },
  },
  {
    id: '12',
    name: 'Детализированный отчет по временным мерам',
    scopes: [
      USER_SCOPES.SYSTEM_ADMINISTRATOR,
      USER_SCOPES.GOMU_SPECIALIST,
      USER_SCOPES.GOMU_OBSERVER,
      USER_SCOPES.MILITARY_COMMISSAR,
      USER_SCOPES.MILITARY_RECRUIT_SPECIALIST,
    ],
    columns: {
      fields: ['created', 'period', 'name', 'download'],
      ...columnsBase,
      create: [
        {
          name: 'regions',
          props: {
            multiple: false,
            clearable: true,
            hidden: true,
          },
        },
        {
          name: 'organizations',
          props: {
            required: false,
            hidden: true,
          },
        },
        {
          name: 'comment',
          props: {
            required: false,
            hidden: true,
          },
        },
        {
          name: 'sortBy',
          props: {
            required: true,
            hidden: false,
          },
        },
        (() =>
          matchScopesForToggleTestOrganisation([USER_SCOPES.SYSTEM_ADMINISTRATOR]) && {
            name: 'test-organisation',
            props: {
              required: true,
              hidden: false,
            },
          })(),
        'range',
        'every',

      ],
      reports: ['id', 'regions', 'organizations', 'comment', 'download'],
      reportsAuto: [
        'id',
        'regions',
        'organizations',

        // 'last',
        'comment',

        'every',
        'downloadAll',
      ],
    },
    source: 'ervu-subpoena-registry',
    queryDefinitions: {
      'c26160d0-1ac6-46a5-96f6-3b94a8980755': (
          report: Report,
          role: reportAccessScope
      ) => true,
    },
  },
  {
    id: '7',
    name: 'Отчет по инцидентам',
    scopes: [
      USER_SCOPES.SYSTEM_ADMINISTRATOR,
      USER_SCOPES.GOMU_SPECIALIST,
      USER_SCOPES.GOMU_OBSERVER,
      USER_SCOPES.MILITARY_COMMISSAR,
      USER_SCOPES.MILITARY_RECRUIT_SPECIALIST,
    ],
    columns: {
      fields: ['created', 'period', 'name', 'download'],
      ...columnsBase,
      create: [
        {
          name: 'regions',
          props: {
            multiple: false,
            clearable: true,
            hidden: true,
          },
        },
        {
          name: 'organizations',
          props: {
            required: false,
            hidden: true,
          },
        },
        {
          name: 'comment',
          props: {
            required: false,
            hidden: true,
          },
        },
        'range',
        'every',
      ],
      reports: ['id', 'regions', 'organizations', 'comment', 'download'],
      reportsAuto: [
        'id',
        'regions',
        'organizations',
        'comment',
        'every',
        'downloadAll',
      ],
    },
    source: 'ervu-incidents-service',
    queryDefinitions: {
      '5fa97ca6-175c-4c9a-a8d3-7f3afc1f779f': (
        report: Report,
        role: reportAccessScope
      ) => true,
    },
  },
  {
    id: '8',
    name: 'Отчет по снятию с воинского учета',
    scopes: [
      USER_SCOPES.MILITARY_COMMISSAR,
      USER_SCOPES.MILITARY_RECRUIT_SPECIALIST,
      USER_SCOPES.GOMU_SPECIALIST,
      USER_SCOPES.GOMU_OBSERVER,
      USER_SCOPES.SYSTEM_ADMINISTRATOR,
    ],
    columns: {
      fields: ['created', 'period', 'name', 'download'],
      ...columnsBase,
      create: [
        {
          name: 'regions',
          props: {
            multiple: false,
            clearable: true,
            hidden: true,
          },
        },
        {
          name: 'organizations',
          props: {
            required: false,
            hidden: true,
          },
        },
        {
          name: 'comment',
          props: {
            required: false,
            hidden: true,
          },
        },
        (() =>
          matchScopesForToggleTestOrganisation([USER_SCOPES.SYSTEM_ADMINISTRATOR]) && {
            name: 'test-organisation',
            props: {
              required: true,
              hidden: false,
            },
          })(),
        'range',
        'every',
      ],
      reports: ['id', 'regions', 'organizations', 'comment', 'download'],
      reportsAuto: [
        'id',
        'regions',
        'organizations',
        'comment',
        'every',
        'downloadAll',
      ],
    },
    source: 'ervu-decision-document-service',
    queryDefinitions: {
      '0f78e543-23ea-4460-a850-1f950909941e': (
        report: Report,
        role: reportAccessScope
      ) => true,
    },
  },
  {
    id: '9',
    name: 'Сводный отчет по уведомлению граждан (ЛК ЕПГУ)',
    scopes: [
      USER_SCOPES.MILITARY_COMMISSAR,
      USER_SCOPES.MILITARY_RECRUIT_SPECIALIST,
      USER_SCOPES.MILITARY_DISTRICT_OBSERVER,
      USER_SCOPES.GOMU_SPECIALIST,
      USER_SCOPES.GOMU_OBSERVER,
      USER_SCOPES.SYSTEM_ADMINISTRATOR,
    ],
    columns: {
      fields: ['created', 'period', 'name', 'download'],
      ...columnsBase,
      create: [
        {
          name: 'regions',
          props: {
            multiple: false,
            clearable: true,
            hidden: true,
          },
        },
        {
          name: 'organizations',
          props: {
            required: false,
            hidden: true,
          },
        },
        {
          name: 'comment',
          props: {
            required: false,
            hidden: true,
          },
        },
        (() =>
          matchScopesForToggleTestOrganisation([USER_SCOPES.SYSTEM_ADMINISTRATOR]) && {
            name: 'test-organisation',
            props: {
              required: true,
              hidden: false,
            },
          })(),
        'range',
        'every',
      ],
      reports: ['id', 'regions', 'organizations', 'comment', 'download'],
      reportsAuto: [
        'id',
        'regions',
        'organizations',
        'comment',
        'every',
        'downloadAll',
      ],
    },
    source: 'personal-template-prefill-service',
    queryDefinitions: {
      '13102528-bf1d-4046-a7d6-a59d63472afc': (
        report: Report,
        role: reportAccessScope
      ) => true,
    },
  },
  {
    id: '10',
    name: 'Сводный отчет по выпискам',
    scopes: [
      USER_SCOPES.GOMU_SPECIALIST,
      USER_SCOPES.GOMU_OBSERVER,
      USER_SCOPES.SYSTEM_ADMINISTRATOR,
    ],
    columns: {
      fields: ['created', 'period', 'name', 'download'],
      ...columnsBase,
      create: [
        {
          name: 'regions',
          props: {
            multiple: false,
            clearable: true,
            hidden: true,
          },
        },
        {
          name: 'organizations',
          props: {
            required: false,
            hidden: true,
          },
        },
        {
          name: 'comment',
          props: {
            required: false,
            hidden: true,
          },
        },
        'range',
        'every',
      ],
      reports: ['id', 'regions', 'organizations', 'comment', 'download'],
      reportsAuto: [
        'id',
        'regions',
        'organizations',
        'comment',
        'every',
        'downloadAll',
      ],
    },
    source: 'ervu-extract-from-registry-provider',
    queryDefinitions: {
      '199fa3a0-b767-46a5-bf00-e5ec5df4c67d': (
        report: Report,
        role: reportAccessScope
      ) => true,
    },
  },
];

export function fetchBaseReports() {
  return new Promise((resolve) => {
    const avelibleReports = baseReports.filter(({ scopes }) =>
      matchScopes(scopes, ScopesMode.or)
    );
    return resolve(cloneDeep(avelibleReports));
  });
}

// для ограничения полей при определенных скоупах
function matchScopesForToggleTestOrganisation(scopes: ERVU_SCOPES[]) {
  return matchScopes(scopes, ScopesMode.or);
}

export function fetchBaseReport(uuid: string) {
  return new Promise((resolve) => {
    return resolve(cloneDeep(baseReports.find(({ id }) => id === uuid)));
  });
}
