import { roleManager } from '@/core/api/classes';
import { ReportScenario } from '@/core/scenarios/Report';
import { WidgetHelper } from '@/core/WidgetHelper';
import { kbEntityManager } from '@/test-data/KB';
import { kbEntityGenerator } from '@/test-data/KB/api';
import { users } from '@/tests/KB/const';
import { test } from '@core/fixtures';

const user = users.authorized;
test.beforeAll(async ({ api: { login } }) => {
  await login();
  await kbEntityManager.flush();
});

test('Report "EAM08 - Vypis kritickych osob"', async ({
  page,
  widgetHelper: wh,
  actions,
  api: { login },
}, testInfo) => {
  (await login(), await roleManager.makeUserSuperuser(user.username));
  await ReportScenario.makeAndRun({
    context: { page, widgetHelper: wh, actions, testInfo },
    user,
    navigateParams: {
      module: 'Modul BIA',
      reportName: 'BIA08 - Výpis kritických osob',
    },
    generateTestData: async () => {
      const entities = await kbEntityGenerator([
        {
          type: 'Company',
          name: 'companyKB',
          data: {
            kod: 'KB',
            nazev: 'Komerční banka',
          },
          priority: 1,
        },

        // ------ OrgUnit

        {
          type: 'OrgUnit',
          name: 'orgunit1',
          data: {
            firma: ({ companyKB }) => companyKB.gid,
            nazev: 'Korporátní a Municipální bankovnictví',
            kod: '2006',
            jiraId: '',
            typUtvaru: 'ARM',
            nazevEn: 'ARM One',
            egjeKod: '02006.016',
            unitId: '2006',
          },
        },
        {
          type: 'OrgUnit',
          name: 'orgunit2',
          data: {
            firma: ({ companyKB }) => companyKB.gid,
            nazev: 'Korporátní centrum - Praha 1',
            kod: '72300',
            unitId: '902019',
            typUtvaru: 'UNIT',
            egjeKod: '72300.300',
            notInEgje: '1',
            notInJira: '1',
            nadrazenyUtvar: ({ orgunit1 }) => orgunit1.gid,
          },
        },
        {
          type: 'OrgUnit',
          name: 'orgunit21',
          data: {
            firma: ({ companyKB }) => companyKB.gid,
            nazev: 'Customer Tribe',
            typUtvaru: 'TRIBE',
            kod: 'cod8931',
            egjeKod: '8931.16',
            unitId: '8931',
            jiraId: '8931',
          },
        },
        {
          type: 'OrgUnit',
          name: 'orgunit22',
          data: {
            firma: ({ companyKB }) => companyKB.gid,
            nazev: 'Client & Onboarding Process',
            kod: 'SQ-36271',
            egjeKod: '',
            unitId: '905',
            typUtvaru: 'SQUAD',
            jiraId: '36271',
            nadrazenyUtvar: ({ orgunit21 }) => orgunit21.gid,
          },
        },
        {
          type: 'OrgUnit',
          name: 'orgunit31',
          data: {
            firma: ({ companyKB }) => companyKB.gid,
            nazev: 'Support Services Tribe',
            typUtvaru: 'TRIBE',
            kod: 'cod9100',
            egjeKod: '9100.16',
            unitId: '9100',
            jiraId: '9100',
          },
        },
        {
          type: 'OrgUnit',
          name: 'orgunit32',
          data: {
            firma: ({ companyKB }) => companyKB.gid,
            nazev: 'Správa vozového parku',
            kod: 'SQ-36371',
            egjeKod: '',
            typUtvaru: 'SQUAD',
            jiraId: '36371',
            nadrazenyUtvar: ({ orgunit31 }) => orgunit31.gid,
          },
        },

        // ------ EaPerson

        {
          type: 'EaPerson',
          name: 'person1',
          data: {
            oscis: '420586',
            lastName: 'Adam',
            firstName: 'Aleš',
            position: 'Procesní analytik',
            email: 'ales.jelinek@kb.com',
            mobile: '420586340502',
            orgUnitCode: ({ orgunit2 }) => orgunit2.unitId,
          },
        },
        {
          type: 'EaPerson',
          name: 'person2',
          data: {
            oscis: '420602',
            lastName: 'Bláha',
            firstName: 'Bořivoj',
            position: 'Development Engineer',
            email: 'robert.psenicka@kb.com',
            mobile: '420602235024',
            orgUnitCode: ({ orgunit2 }) => orgunit2.unitId,
          },
        },
        {
          type: 'EaPerson',
          name: 'person3',
          data: {
            oscis: '602235',
            lastName: 'Císař',
            firstName: 'Cyril',
            position: 'Development Engineer',
            email: 'cyril.cisar@kb.com',
            mobile: '420602235024',
            orgUnitCode: ({ orgunit2 }) => orgunit2.unitId,
          },
        },

        // ------ CapabilityDomain

        {
          type: 'CapabilityDomain',
          name: 'capabilitydomain1',
          data: {
            nameCz: 'Řízení trhu, nabídky a poptávky',
            kod: 'D1',
            domainObjectCz: 'Trh, nabídka, poptávka',
            company: 'KB',
            firma: ({ companyKB }) => companyKB.gid,
          },
        },
        {
          type: 'CapabilityDomain',
          name: 'capabilitydomain11',
          data: {
            nameCz: 'Řízení informací o trhu',
            kod: 'D11',
            domainObjectCz: 'Trh',
            parentDomain: ({ capabilitydomain1 }) => capabilitydomain1.gid,
          },
        },
        {
          type: 'CapabilityDomain',
          name: 'capabilitydomain111',
          data: {
            nameCz: 'Analýza tržních informací',
            kod: 'D111',
            domainObjectCz: 'Trh',
            parentDomain: ({ capabilitydomain11 }) => capabilitydomain11.gid,
          },
        },
        {
          type: 'CapabilityDomain',
          name: 'capabilitydomain112',
          data: {
            nameCz: 'Analýza konkurence',
            kod: 'D112',
            domainObjectCz: 'Trh',
            parentDomain: ({ capabilitydomain11 }) => capabilitydomain11.gid,
          },
        },

        // ------ Capability

        {
          type: 'Capability',
          name: 'capability1',
          data: {
            nazevCz: 'Capabilita direct bankingu pro corporátní management',
            nazevEn: 'Direct banking products Corp. Cash management',
            code: 'D420.69',
            wfState: '100',
            vlastnik: ({ orgunit21 }) => orgunit21.gid,
            spravce: ({ orgunit22 }) => orgunit22.gid,
            domena: ({ capabilitydomain111 }) => capabilitydomain111.gid,
          },
        },
        {
          type: 'Capability',
          name: 'capability2',
          data: {
            nazevCz: 'Produkty přímého bankovnictví Corp. Cash management',
            nazevEn: 'Direct banking products Corp. Cash management',
            code: 'D7.000',
            wfState: '010',
            vlastnik: ({ orgunit31 }) => orgunit31.gid,
            spravce: ({ orgunit32 }) => orgunit32.gid,
            domena: ({ capabilitydomain112 }) => capabilitydomain112.gid,
          },
        },
        {
          type: 'Capability',
          name: 'capability3',
          data: {
            nazevCz: 'Produkty přímého bankovnictví',
            nazevEn: 'Direct banking products',
            code: 'D1.110/B',
            wfState: '950',
            vlastnik: ({ orgunit31 }) => orgunit31.gid,
            spravce: ({ orgunit32 }) => orgunit32.gid,
            domena: ({ capabilitydomain112 }) => capabilitydomain112.gid,
          },
        },

        // ------ LogicalDomainRoot

        {
          type: 'LogicalDomainRoot',
          name: 'ldr1',
          data: {
            code: 'LDR-1',
            registryCode: 'LDR_REG_1',
            nameCz: 'Logická doména',
            nameEn: 'Logical Domain',
          },
          priority: 1,
        },
        {
          type: 'LogicalBusinessObject',
          name: 'lbo1',
          data: {
            code: 'LBO-1',
            nameCz: 'Business objekt',
            nameEn: 'Business Object',
            domainRoot: ({ ldr1 }) => ldr1.gid,
          },
        },

        // ------ Agenda

        {
          type: 'Agenda',
          name: 'agenda4',
          data: {
            kod: 'AG-4',
            nazevCz: 'BigBangTheory',
            capabilita: ({ capability1 }) => capability1.gid,
            'Business object': ({ lbo1 }) => lbo1.gid,
            biaKriticnost: 'vital',
          },
        },
        {
          type: 'Agenda',
          name: 'agenda5',
          data: {
            kod: 'AG1-5',
            nazevCz: 'Chess',
            capabilita: ({ capability2 }) => capability2.gid,
            'Business object': ({ lbo1 }) => lbo1.gid,
          },
        },
        {
          type: 'Agenda',
          name: 'agenda6',
          data: {
            kod: 'AG1-6',
            nazevCz: 'CryptoCoin',
            capabilita: ({ capability2 }) => capability2.gid,
            'Business object': ({ lbo1 }) => lbo1.gid,
          },
        },
        {
          type: 'Agenda',
          name: 'agenda7',
          data: {
            kod: 'AG1-7',
            nazevCz: 'LOL',
            capabilita: ({ capability3 }) => capability3.gid,
            'Business object': ({ lbo1 }) => lbo1.gid,
            biaKriticnost: 'critical',
          },
        },

        // ------ AgendaXPerson

        {
          type: 'AgendaXPerson',
          name: 'agendaxperson1',
          data: {
            agenda: ({ agenda4 }) => agenda4.gid,
            person: ({ person1 }) => `person-${person1.oscis}`,
            cpImportancy: 'Primary', // "Primární"
          },
        },
        {
          type: 'AgendaXPerson',
          name: 'agendaxperson2',
          data: {
            agenda: ({ agenda6 }) => agenda6.gid,
            person: ({ person2 }) => `person-${person2.oscis}`,
            cpImportancy: 'Deputy', // "Zástupce"
          },
        },
        {
          type: 'AgendaXPerson',
          name: 'agendaxperson3',
          data: {
            agenda: ({ agenda7 }) => agenda7.gid,
            person: ({ person3 }) => `person-${person3.oscis}`,
            cpImportancy: 'Primary',
          },
        },
        {
          type: 'AgendaXPerson',
          name: 'agendaxperson_agenda5',
          data: {
            agenda: ({ agenda5 }) => agenda5.gid,
            person: ({ person1 }) => `person-${person1.oscis}`,
            cpImportancy: 'Primary',
          },
        },

        // ------ AgendaXOrgUnitRealizator

        {
          type: 'AgendaXOrgUnitRealizator',
          name: 'agnRealizator1',
          data: {
            agenda: ({ agenda4 }) => agenda4.gid,
            orgUnit: ({ orgunit1 }) => orgunit1.gid,
          },
        },
        {
          type: 'AgendaXOrgUnitRealizator',
          name: 'agnRealizator2',
          data: {
            agenda: ({ agenda5 }) => agenda5.gid,
            orgUnit: ({ orgunit2 }) => orgunit2.gid,
          },
        },
        {
          type: 'AgendaXOrgUnitRealizator',
          name: 'agnRealizator3',
          data: {
            agenda: ({ agenda6 }) => agenda6.gid,
            orgUnit: ({ orgunit21 }) => orgunit21.gid,
          },
        },
        {
          type: 'AgendaXOrgUnitRealizator',
          name: 'agnRealizator4',
          data: {
            agenda: ({ agenda7 }) => agenda7.gid,
            orgUnit: ({ orgunit31 }) => orgunit31.gid,
          },
        },
      ]);

      // ------

      await kbEntityManager.update('OrgUnit', entities.get('orgunit1').gid, {
        typUtvaru: 'ARM',
        unitId: '2006',
      });
      await kbEntityManager.update('OrgUnit', entities.get('orgunit2').gid, {
        unitId: '902019',
      });
      await kbEntityManager.update('OrgUnit', entities.get('orgunit21').gid, {
        typUtvaru: 'TRIBE',
        unitId: '8931',
      });
      await kbEntityManager.update('OrgUnit', entities.get('orgunit31').gid, {
        typUtvaru: 'TRIBE',
        unitId: '9100',
      });
      await kbEntityManager.update('EaPerson', entities.get('person1').gid, {
        orgUnitCode: '902019',
      });
      await kbEntityManager.update('EaPerson', entities.get('person2').gid, {
        orgUnitCode: '902019',
      });
      await kbEntityManager.update('EaPerson', entities.get('person3').gid, {
        orgUnitCode: '902019',
      });
      return entities;
    },

    // ------

    fillReportFormFunc: async () => {
      await wh.checkboxInput({ label: 'Celá organizace' }).check();
      await wh.checkboxInput({ label: 'Včetně podútvarů' }).check();

      await wh
        .selectInput({
          label:
            'Organizace (použije se jen pokud je zaškrtnuta volba "Celá organizace") ',
        })
        .select('KB');

      // await page.waitForTimeout(1000); // Pro DEBUG
    },

    verifyExcelFileData(api, excelApi) {
      const { rowExists, cellContain } = excelApi;
      const {
        companyKB,
        person1,
        person2,
        person3,
        agenda4,
        agenda5,
        agenda6,
        agenda7,
        capability1,
        capability2,
        capability3,
        orgunit1,
        orgunit2,
        orgunit21,
        orgunit31,
      } = api.data;

      /*
      // DEBUG - pouze pro testovací účely
      const { workbook } = excelApi;
      const worksheet = workbook.getWorksheet(1);
      if (!worksheet) {
        console.log('Worksheet nenalezen!');
        return;
      }
      console.log('=== OBSAH EXCELU ===');
      console.log('Počet řádků:', worksheet.rowCount);
      for (let row = 1; row <= Math.min(20, worksheet.rowCount); row++) {
        const rowData = worksheet.getRow(row);
        const values: string[] = [];
        rowData.eachCell({ includeEmpty: true }, (cell, colNumber) => {
          values.push(`[${colNumber}]: ${cell.text}`);
        });
        console.log(`Řádek ${row}: ${values.join(' | ')}`);
      }
      console.log('====================');
      // DEBUG - konec
      */

      test.step('Kontrola počtu řádků', () => {
        rowExists(1, 4); // header + 4 data rows
      });

      test.step('Kontrola záhlaví', () => {
        cellContain(1, 3, 1, 'Company');
        cellContain(1, 3, 2, 'Úsek / Tribe');
        cellContain(1, 3, 3, 'Útvar');
        cellContain(1, 3, 4, 'Oscis');
        cellContain(1, 3, 5, 'Příjmení');
        cellContain(1, 3, 6, 'Jméno');
        cellContain(1, 3, 7, 'Důležitost');
        cellContain(1, 3, 8, 'Pozice');
        cellContain(1, 3, 9, 'E - mail');
        cellContain(1, 3, 10, 'Telefon');
        cellContain(1, 3, 11, 'Kritičnost agendy');
        cellContain(1, 3, 12, 'Kód Agendy');
        cellContain(1, 3, 13, 'Agenda');
        cellContain(1, 3, 14, 'Kód Capability');
        cellContain(1, 3, 15, 'Capabilita');
        cellContain(1, 3, 16, 'Function');
        cellContain(1, 3, 17, 'Function Criticality');
        cellContain(1, 3, 18, 'Business Line');
        cellContain(1, 3, 19, 'BL Essentiality');
        cellContain(1, 3, 20, 'EBA Service Type');
      });

      test.step('Kontrola řádek 1 - person1/agenda4', () => {
        cellContain(1, 4, 1, companyKB.kod);
        cellContain(1, 4, 2, String(orgunit1.kod + ' - ' + orgunit1.nazev));
        cellContain(1, 4, 3, orgunit2.nazev);
        cellContain(1, 4, 4, String(person1.oscis));
        cellContain(1, 4, 5, person1.lastName);
        cellContain(1, 4, 6, person1.firstName);
        cellContain(1, 4, 7, 'Primární');
        cellContain(1, 4, 8, person1.position);
        cellContain(1, 4, 9, person1.email);
        cellContain(1, 4, 10, person1.mobile);
        cellContain(1, 4, 11, 'vital'); // biaKriticnost z agenda4
        cellContain(1, 4, 12, 'AG420.69-01');
        cellContain(1, 4, 13, agenda4.nazevCz);
        cellContain(1, 4, 14, capability1.code);
        cellContain(1, 4, 15, capability1.nazevCz);
        // cellContain(1, 4, 16, ...); // Function - prázdné
        cellContain(1, 4, 17, 'No');
        // cellContain(1, 4, 18, ...); // Business Line - prázdné
        cellContain(1, 4, 19, 'No');
        // cellContain(1, 4, 20, ...); // EBA Service Type - prázdné
      });

      test.step('Kontrola řádek 2 - person1/agenda5', () => {
        cellContain(1, 5, 1, companyKB.kod);
        cellContain(1, 5, 2, String(orgunit1.kod + ' - ' + orgunit1.nazev));
        cellContain(1, 5, 3, orgunit2.nazev);
        cellContain(1, 5, 4, String(person1.oscis));
        cellContain(1, 5, 5, person1.lastName);
        cellContain(1, 5, 6, person1.firstName);
        cellContain(1, 5, 7, 'Primární');
        cellContain(1, 5, 8, person1.position);
        cellContain(1, 5, 9, person1.email);
        cellContain(1, 5, 10, person1.mobile);
        // cellContain(1, 5, 11, ...); // biaKriticnost - agenda5 nemá nastaveno
        cellContain(1, 5, 12, 'AG7.000-01');
        cellContain(1, 5, 13, agenda5.nazevCz);
        cellContain(1, 5, 14, capability2.code);
        cellContain(1, 5, 15, capability2.nazevCz);
        // cellContain(1, 5, 16, ...); // Function - prázdné
        cellContain(1, 5, 17, 'No');
        // cellContain(1, 5, 18, ...); // Business Line - prázdné
        cellContain(1, 5, 19, 'No');
        // cellContain(1, 5, 20, ...); // EBA Service Type - prázdné
      });

      test.step('Kontrola řádek 3 - person2/agenda6', () => {
        cellContain(1, 6, 1, companyKB.kod);
        cellContain(1, 6, 2, String(orgunit1.kod + ' - ' + orgunit1.nazev));
        cellContain(1, 6, 3, orgunit2.nazev);
        cellContain(1, 6, 4, String(person2.oscis));
        cellContain(1, 6, 5, person2.lastName);
        cellContain(1, 6, 6, person2.firstName);
        cellContain(1, 6, 7, 'Zástupce'); // Deputy
        cellContain(1, 6, 8, person2.position);
        cellContain(1, 6, 9, person2.email);
        cellContain(1, 6, 10, person2.mobile);
        // cellContain(1, 6, 11, ...); // biaKriticnost - agenda6 nemá nastaveno
        cellContain(1, 6, 12, 'AG7.000-02');
        cellContain(1, 6, 13, agenda6.nazevCz);
        cellContain(1, 6, 14, capability2.code);
        cellContain(1, 6, 15, capability2.nazevCz);
        // cellContain(1, 6, 16, ...); // Function - prázdné
        cellContain(1, 6, 17, 'No');
        // cellContain(1, 6, 18, ...); // Business Line - prázdné
        cellContain(1, 6, 19, 'No');
        // cellContain(1, 6, 20, ...); // EBA Service Type - prázdné
      });

      test.step('Kontrola řádek 4 - person3/agenda7', () => {
        cellContain(1, 7, 1, companyKB.kod);
        cellContain(1, 7, 2, String(orgunit1.kod + ' - ' + orgunit1.nazev));
        cellContain(1, 7, 3, orgunit2.nazev);
        cellContain(1, 7, 4, String(person3.oscis));
        cellContain(1, 7, 5, person3.lastName);
        cellContain(1, 7, 6, person3.firstName);
        cellContain(1, 7, 7, 'Primární');
        cellContain(1, 7, 8, person3.position);
        cellContain(1, 7, 9, person3.email);
        cellContain(1, 7, 10, person3.mobile);
        cellContain(1, 7, 11, 'critical'); // biaKriticnost z agenda7
        cellContain(1, 7, 12, 'AG1.110/B-01');
        cellContain(1, 7, 13, agenda7.nazevCz);
        cellContain(1, 7, 14, capability3.code);
        cellContain(1, 7, 15, capability3.nazevCz);
        // cellContain(1, 7, 16, ...); // Function - prázdné
        cellContain(1, 7, 17, 'No');
        // cellContain(1, 7, 18, ...); // Business Line - prázdné
        cellContain(1, 7, 19, 'No');
        // cellContain(1, 7, 20, ...); // EBA Service Type - prázdné
      });
    },
  });
});
