import { expect } from '@playwright/test';
import { test } from '../../../fixtures/group.fixture';
import { ReviewPage } from '../../../pages/review.page';
import { SearchPage } from '../../../pages/search.page';
import { SingleSequenceSubmissionPage } from '../../../pages/submission.page';
import { randomUUID } from 'crypto';

test('Override hidden fields', async ({ page, groupId }) => {
    // This test is really slow - it can take at least 150s. Speed depends on how much else is running, hence let's use buffer.
    test.setTimeout(200_000);
    void groupId;
    const uuid = randomUUID();

    await page.goto('/');
    const submissionPage = new SingleSequenceSubmissionPage(page);
    await submissionPage.completeSubmission(
        {
            submissionId: 'foo1',
            collectionCountry: 'France',
            collectionDate: '2023-10-15',
            authorAffiliations: uuid,
        },
        {
            fastaHeader:
                'ATTGATCTCATCATTTACCAATTGGAGACCGTTTAACTAGTCAATCCCCCATTTGGGGGCATTCCTAAAGTGTTGCAAAGGTATGTGGGTCGTATTGCTTTGCCTTTTCCTAACCTGGCTCCTCCTACAATTCTAACCTGCTTGATAAGTGTGATTACCTGAGTAATAGACTAATTTCGTCCTGGTAATTAGCATTTTCTAGTAAAACCAATACTATCTCAAGTCCTAAGAGGAGGTGAGAAGAGGGTCTCGAGGTATCCCTCCAGTCCACAAAATCTAGCTAATTTTAGCTGAGTGGACTGATTACTCTCATCACACGCTAACTACTAAGGGTTTACCTGAGAGCCTACAACATGGATAAACGGGTGAGAGGTTCATGGGCCCTGGGAGGACAATCTGAAGTTGATCTTGACTACCACAAAATATTAACAGCCGGGCTTTCGGTCCAACAAGGGATTGTGCGACAAAGAGTCATCCCGGTATATGTTGTGAGTGATCTTGAGGGTATTTGTCAACATATCATTCAGGCCTTTGAAGCAGGCGTAGATTTCCAAGATAATGCTGACAGCTTCCTTTTACTTTTATGTTTACATCATGCTTACCAAGGAGATCATAGGCTCTTCCTCAAAAGTGATGCAGTTCAATACTTAGAGGGCCATGGTTTCAGGTTTGAGGTCCGAGAAAAGGAGAATGTGCACCGTCTGGATGAATTGTTGCCCAATGTCACCGGTGGAAAAAATCTTAGGAGAACATTGGCTGCAATGCCTGAAGAGGAGACAACAGAAGCTAACGCTGGTCAGTTTTTATCCTTTGCCAGTTTGTTTCTACCCAAACTTGTCGTTGGGGAGAAAGCGTGTCTGGAAAAAGTACAAAGGCAGATTCAGGTCCATGCAGAACAAGGGCTCATTCAATATCCAACTTCCTGGCAATCAGTTGGACACATGATGGTGATCTTCCGTTTGATGAGAACAAACTTTTTAATCAAGTTCCTACTAATACATCAGGGGATGCACATGG',
        },
    );
    await page.goto('/');
    let reviewPage = await submissionPage.completeSubmission(
        {
            submissionId: 'foo1',
            collectionCountry: 'Uganda',
            collectionDate: '2023-10-15',
            authorAffiliations: uuid,
        },
        {
            fastaHeader:
                'ATTGATCTCATCATTTACCAATTGGAGACCGTTTAACTAGTCAATCCCCCATTTGGGGGCATTCCTAAAGTGTTGCAAAGGTATGTGGGTCGTATTGCTTTGCCTTTTCCTAACCTGGCTCCTCCTACAATTCTAACCTGCTTGATAAGTGTGATTACCTGAGTAATAGACTAATTTCGTCCTGGTAATTAGCATTTTCTAGTAAAACCAATACTATCTCAAGTCCTAAGAGGAGGTGAGAAGAGGGTCTCGAGGTATCCCTCCAGTCCACAAAATCTAGCTAATTTTAGCTGAGTGGACTGATTACTCTCATCACACGCTAACTACTAAGGGTTTACCTGAGAGCCTACAACATGGATAAACGGGTGAGAGGTTCATGGGCCCTGGGAGGACAATCTGAAGTTGATCTTGACTACCACAAAATATTAACAGCCGGGCTTTCGGTCCAACAAGGGATTGTGCGACAAAGAGTCATCCCGGTATATGTTGTGAGTGATCTTGAGGGTATTTGTCAACATATCATTCAGGCCTTTGAAGCAGGCGTAGATTTCCAAGATAATGCTGACAGCTTCCTTTTACTTTTATGTTTACATCATGCTTACCAAGGAGATCATAGGCTCTTCCTCAAAAGTGATGCAGTTCAATACTTAGAGGGCCATGGTTTCAGGTTTGAGGTCCGAGAAAAGGAGAATGTGCACCGTCTGGATGAATTGTTGCCCAATGTCACCGGTGGAAAAAATCTTAGGAGAACATTGGCTGCAATGCCTGAAGAGGAGACAACAGAAGCTAACGCTGGTCAGTTTTTATCCTTTGCCAGTTTGTTTCTACCCAAACTTGTCGTTGGGGAGAAAGCGTGTCTGGAAAAAGTACAAAGGCAGATTCAGGTCCATGCAGAACAAGGGCTCATTCAATATCCAACTTCCTGGCAATCAGTTGGACACATGATGGTGATCTTCCGTTTGATGAGAACAAACTTTTTAATCAAGTTCCTACTAATACATCAGGGGATGCACATGG',
        },
    );

    await reviewPage.waitForZeroProcessing();
    await reviewPage.releaseAndGoToReleasedSequences();

    while (!(await page.getByText('Search returned 2 sequences').isVisible())) {
        await page.reload();
        await page.waitForTimeout(2000);
    }

    let search = new SearchPage(page);
    await search.ebolaSudan();

    // This is just to ensure that things are interactive and ready - bit of a hack for now
    await search.select('Collection country', 'France');
    await page.getByLabel('Clear').click();

    await search.enableSearchFields('Author affiliations');
    await search.fill('Author affiliations', uuid);

    await page.getByRole('cell', { name: 'France' }).click();
    await search.reviseSequence();
    await page.getByLabel('Collection date').fill('2012-12-13');
    await page.getByRole('button', { name: /proceed to Approval/ }).click();
    await page.getByRole('button', { name: 'Confirm' }).click();
    await expect(page.getByText('Review pending submissions')).toBeVisible();

    search = new SearchPage(page);
    await search.ebolaSudan();

    await search.select('Collection country', 'Uganda');
    await search.enableSearchFields('Author affiliations');
    await search.fill('Author affiliations', uuid);
    await search.expectSequenceCount(1);
    const revokedId = await page.getByRole('link', { name: /LOC_[A-Z0-9]{2,20}\.1/ }).textContent();
    const revokedAccession = revokedId.split('.')[0];
    const expectedRevocationAccessionVersion = `${revokedAccession}.2`;
    await page.getByRole('cell', { name: 'Uganda' }).click();
    await search.revokeSequence();

    // The revocation is auto-approved; only the revision still needs to be released
    reviewPage = new ReviewPage(page);
    await reviewPage.navigateToReviewPage();
    await reviewPage.waitForZeroProcessing();
    await reviewPage.releaseAndGoToReleasedSequences();
    while (!(await page.getByRole('cell', { name: '2012-12-13' }).isVisible())) {
        await page.reload();
        await page.waitForTimeout(2000);
    }

    await search.ebolaSudan();

    // This is just to ensure that things are interactive and ready - bit of a hack for now
    await search.select('Collection country', 'France');
    await page.getByLabel('Clear').click();

    await search.enableSearchFields('Author affiliations', 'Version status');
    await search.fill('Author affiliations', uuid);

    await search.expectSequenceCount(1);
    await search.clearSelect('Version status');
    await search.expectSequenceCount(3);
    await search.select('Collection country', 'France');
    await search.expectSequenceCount(2);

    await page.getByRole('button', { name: 'Reset' }).click();
    await search.enableSearchFields('Is revocation');
    await search.select('Is revocation', 'true');
    await search.fill('Accession', revokedAccession, true);
    await expect(
        page.getByRole('link', { name: expectedRevocationAccessionVersion }),
    ).toBeVisible();
});
