/*
 * (c) 2023 Pluto Inc.
 */
package tv.pluto;

import io.qameta.allure.Description;
import io.qameta.allure.TmsLink;
import java.time.Instant;
import java.util.List;
import org.bson.types.ObjectId;
import org.testng.Assert;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
import tv.pluto.core.BaseTest;
import tv.pluto.grpc.timelines_past.TimelinesPastClient;
import tv.pluto.mongo.entities.timelinespast.TimelinesPast;
import tv.pluto.mongo.utils.TimelinesPastUtils;
import tv.pluto.utils.CommonUtils;

public class TimelinesPastTestSuite extends BaseTest {
  private TimelinesPastClient timelinesPastClient;
  private TimelinesPastUtils timelinesPastUtils;

  @BeforeClass(alwaysRun = true)
  private void setUp() {
    timelinesPastClient = new TimelinesPastClient(grpcChannelManager.getChannel());
    timelinesPastUtils = new TimelinesPastUtils();
  }

  @Test
  @Description("FindByIdAndUpsert - update an existing timelines-past")
  @TmsLink("28501577")
  public void C28501577_TEST() {
    TimelinesPast timelinesPast =
        timelinesPastUtils.getRandomDocumentWithTheField("createdAt", true);
    Instant newCreatedTime = timelinesPast.getCreatedAt().toInstant().minusSeconds(40);
    Instant newStart = timelinesPast.getStart().toInstant().minusSeconds(10);
    Instant newStop = timelinesPast.getStop().toInstant().minusSeconds(10);

    tv.pluto.ip.dal.timelinespast.TimelinesPast.TimelinePast timelinepast =
        tv.pluto.ip.dal.timelinespast.TimelinesPast.TimelinePast.newBuilder()
            .setId(timelinesPast.getId())
            .setCreatedAt(newCreatedTime.toString())
            .setChannel(timelinesPast.getChannel())
            .setStart(newStart.toString())
            .setStop(newStop.toString())
            .setEpisode(timelinesPast.getEpisode())
            .setFeatured(!timelinesPast.getFeatured())
            .build();

    List<String> values = List.of("createdAt", "start", "stop", "featured");
    tv.pluto.ip.dal.timelinespast.TimelinesPast.FindByIdAndUpsertResponse timelinePast =
        timelinesPastClient.getFindByIdAndUpsert(timelinesPast.getId(), timelinepast, values);

    TimelinesPast timelinesPastGrpc =
        timelinesPastUtils.mapGrpcObjectToPojo(timelinePast.getDocument(), TimelinesPast.class);
    TimelinesPast mangoDBResponse = timelinesPastUtils.getDocumentById(timelinesPast.getId());

    Assert.assertEquals(timelinesPastGrpc.getCreatedAt().toInstant(), newCreatedTime);
    Assert.assertEquals(timelinesPastGrpc.getStart().toInstant(), newStart);
    Assert.assertEquals(timelinesPastGrpc.getStop().toInstant(), newStop);
    Assert.assertNotEquals(timelinesPastGrpc.getFeatured(), timelinesPast.getFeatured());
    Assert.assertEquals(timelinesPastGrpc.getChannel(), timelinesPast.getChannel());
    Assert.assertEquals(timelinesPastGrpc.getEpisode(), timelinesPast.getEpisode());
    Assert.assertEquals(timelinesPastGrpc, mangoDBResponse);
  }

  @Test
  @Description("FindByIdAndUpsert - create a new timelinesPast")
  @TmsLink("28501578")
  public void C28501578_TEST() {
    String nonExistentId = new ObjectId().toHexString();

    Assert.assertNull(timelinesPastUtils.getDocumentById(nonExistentId));

    TimelinesPast timelinesPast =
        timelinesPastUtils.getRandomDocumentWithTheField("createdAt", true);

    tv.pluto.ip.dal.timelinespast.TimelinesPast.TimelinePast timelinepast =
        tv.pluto.ip.dal.timelinespast.TimelinesPast.TimelinePast.newBuilder()
            .setId(nonExistentId)
            .setCreatedAt(timelinesPast.getCreatedAt().toInstant().toString())
            .setChannel(timelinesPast.getChannel())
            .setStart(timelinesPast.getStart().toInstant().toString())
            .setStop(timelinesPast.getStop().toInstant().toString())
            .setEpisode(timelinesPast.getEpisode())
            .setFeatured(!timelinesPast.getFeatured())
            .build();

    tv.pluto.ip.dal.timelinespast.TimelinesPast.FindByIdAndUpsertResponse timelinePast =
        timelinesPastClient.getFindByIdAndUpsert(nonExistentId, timelinepast, List.of(""));

    TimelinesPast timelinesPastGrpc =
        timelinesPastUtils.mapGrpcObjectToPojo(timelinePast.getDocument(), TimelinesPast.class);
    TimelinesPast mangoDBResponse = timelinesPastUtils.getDocumentById(timelinesPastGrpc.getId());

    Instant timeBefore = Instant.now().minusSeconds(60);
    Instant timeNow = Instant.now();

    boolean isDateBetween =
        CommonUtils.isDateBetweenDates(
            timeBefore, timeNow, timelinesPastGrpc.getCreatedAt().toInstant());

    Assert.assertTrue(isDateBetween);
    Assert.assertNotEquals(timelinesPastGrpc.getFeatured(), timelinesPast.getFeatured());
    Assert.assertEquals(timelinesPastGrpc.getChannel(), timelinesPast.getChannel());
    Assert.assertEquals(timelinesPastGrpc.getEpisode(), timelinesPast.getEpisode());
    Assert.assertEquals(timelinesPastGrpc, mangoDBResponse);
    timelinesPastUtils.deleteDocumentById(nonExistentId);
  }

  @Test
  @Description("FindByIdAndUpsert - invalid request")
  @TmsLink("28501579")
  public void C28501579_TEST() {
    String timelineId = "000009880555556600000999";
    tv.pluto.ip.dal.timelinespast.TimelinesPast.TimelinePast timelinepast =
        tv.pluto.ip.dal.timelinespast.TimelinesPast.TimelinePast.newBuilder()
            .setId(timelineId)
            .build();

    timelinesPastClient.getFindByIdAndUpsert(timelineId, timelinepast, List.of(""));
  }
}
