package io.delphiplatform.api.v3.bigtable.processing;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

import java.time.LocalDate;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import io.delphiplatform.api.v3.model.ArtistFollower;
import io.delphiplatform.api.v3.model.GroupByField;
import io.delphiplatform.api.v3.view.util.Params;

class ArtistFollowerAggregatorTest {

    private ArtistFollowerAggregator artistFollowerAggregator = new ArtistFollowerAggregator();

    @Test
    void groupBy_date() {
        LocalDate first = LocalDate.of(2020, 1, 1);
        LocalDate second = LocalDate.of(2020, 1, 2);
        LocalDate third = LocalDate.of(2020, 1, 3);
        List<ArtistFollower> grouped = artistFollowerAggregator.groupBy(Stream.of(
            new ArtistFollower().date(first).appleFollowers(1L),
            new ArtistFollower().date(second).appleFollowers(1L),
            new ArtistFollower().date(second).appleFollowers(1L),
            new ArtistFollower().date(third).appleFollowers(10L)
        ), Params.builder().groupByField(GroupByField.DATE).build())
            .sorted(Comparator.comparing(ArtistFollower::getDate))
            .collect(Collectors.toList());

        Assertions.assertEquals(3, grouped.size());
        Assertions.assertEquals(first, grouped.get(0).getDate());
        Assertions.assertEquals(1L, grouped.get(0).getAppleFollowers());

        Assertions.assertEquals(second, grouped.get(1).getDate());
        Assertions.assertEquals(2L, grouped.get(1).getAppleFollowers());

        Assertions.assertEquals(third, grouped.get(2).getDate());
        Assertions.assertEquals(10L, grouped.get(2).getAppleFollowers());
    }

    @Test
    void groupBy_noFields() {
        LocalDate first = LocalDate.of(2020, 1, 1);
        LocalDate second = LocalDate.of(2020, 1, 2);
        LocalDate third = LocalDate.of(2020, 1, 3);
        List<ArtistFollower> grouped = artistFollowerAggregator.groupBy(Stream.of(
            new ArtistFollower().date(first).appleFollowers(1L),
            new ArtistFollower().date(second).appleFollowers(1L).facebookLikes(10L),
            new ArtistFollower().date(second).appleFollowers(1L).spotifyFollowers(100L),
            new ArtistFollower().date(third).appleFollowers(1L).facebookLikes(10L)
        ), Params.builder().build())
            .sorted(Comparator.comparing(ArtistFollower::getDate))
            .collect(Collectors.toList());

        Assertions.assertEquals(1, grouped.size());
        Assertions.assertNull(grouped.get(0).getDate());
        Assertions.assertEquals(4L, grouped.get(0).getAppleFollowers());
        Assertions.assertEquals(20L, grouped.get(0).getFacebookLikes());
        Assertions.assertEquals(100L, grouped.get(0).getSpotifyFollowers());
    }
}