package com.iqser.red.service.peristence.v1.server.integration.tests;

import static org.assertj.core.api.Assertions.assertThat;

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;

import com.iqser.red.service.peristence.v1.server.integration.client.FileClient;
import com.iqser.red.service.peristence.v1.server.integration.client.ViewedPagesClient;
import com.iqser.red.service.peristence.v1.server.integration.service.DossierTesterAndProvider;
import com.iqser.red.service.peristence.v1.server.integration.service.FileTesterAndProvider;
import com.iqser.red.service.peristence.v1.server.integration.service.UserProvider;
import com.iqser.red.service.peristence.v1.server.integration.utils.AbstractPersistenceServerServiceTest;
import com.iqser.red.service.persistence.service.v1.api.shared.model.ViewedPagesRequest;
import com.iqser.red.service.persistence.service.v1.api.shared.model.annotations.ViewedPage;
import com.iqser.red.service.persistence.service.v1.api.shared.model.common.JSONPrimitive;

public class ViewedPagesTest extends AbstractPersistenceServerServiceTest {

    @Autowired
    private FileTesterAndProvider fileTesterAndProvider;

    @Autowired
    private DossierTesterAndProvider dossierTesterAndProvider;

    @Autowired
    private ViewedPagesClient viewedPagesClient;

    @Autowired
    private UserProvider userProvider;

    @Autowired
    private FileClient fileClient;


    @Test
    public void testViewedPages() {

        var dossier = dossierTesterAndProvider.provideTestDossier();

        var file = fileTesterAndProvider.testAndProvideFile(dossier);
        var userId = userProvider.getUserId();

        fileClient.setCurrentFileAssignee(dossier.getId(), file.getId(), userId);
        var fileId = file.getId();

        viewedPagesClient.addPage(dossier.getId(),fileId, new ViewedPagesRequest(1));
        viewedPagesClient.addPage(dossier.getId(),fileId,  new ViewedPagesRequest(2));
        viewedPagesClient.addPage(dossier.getId(),fileId,  new ViewedPagesRequest(3));

        viewedPagesClient.removePage(dossier.getId(),fileId, 1);

        var viewedPages = viewedPagesClient.getViewedPages(dossier.getId(),fileId);
        assertThat(viewedPages.getPages().stream().map(ViewedPage::getPage)).containsExactlyInAnyOrder(2, 3);

        viewedPagesClient.removePage(dossier.getId(),fileId, 2);
        viewedPagesClient.removePage(dossier.getId(),fileId,  3);

        viewedPages = viewedPagesClient.getViewedPages(dossier.getId(),fileId);
        assertThat(viewedPages.getPages()).isEmpty();

    }

}
