package com.orchard.models;

import static org.junit.jupiter.api.Assertions.assertEquals;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.kafka.connect.data.Struct;
import org.junit.jupiter.api.Test;

public class GlobalParticipantTests {

    public static final String GlobalParticipantJson =
        "{\"name\":\"test\","
        + "\"id\":\"testid\","
        + "\"imageUrl\":\"testurl\","
        + "\"countryCode\":\"US\","
        + "\"hasPublicData\":true,"
        + "\"createdAt\":\"testdate\","
        + "\"lastModifiedAt\":\"testdate\","
        + "\"stats\":"
        + "{\"followers\":1,"
        + "\"monthlyListeners\":2,"
        + "\"popularity\":3,"
        + "\"views\":4},"
        + "\"socialAccountIds\":"
        + "{\"spotify\":\"testspotifyid\","
        + "\"appleMusic\":\"testappleid\","
        + "\"chartmetric\":1}}";

    public static final GlobalParticipant CreateGlobalParticipant()
    {
        GlobalParticipant globalParticipant = new GlobalParticipant();
        globalParticipant.name = "test";
        globalParticipant.id = "testid";
        globalParticipant.imageUrl = "testurl";
        globalParticipant.countryCode = "US"; 
        globalParticipant.hasPublicData = true;

        globalParticipant.stats = new Stats();
        globalParticipant.stats.followers = 1;
        globalParticipant.stats.monthlyListeners = 2;
        globalParticipant.stats.popularity = 3;
        globalParticipant.stats.views = 4;

        globalParticipant.socialAccountIds = new GPSocialAccountIds();
        globalParticipant.socialAccountIds.spotify = "testspotifyid";
        globalParticipant.socialAccountIds.appleMusic = "testappleid";
        globalParticipant.socialAccountIds.chartmetric = 1;

        return globalParticipant;
    }

    @Test
    public void shouldReturnValidKafkaStruct() throws JsonProcessingException {
        ObjectMapper mapper = new ObjectMapper();
        GlobalParticipant globalParticipant = CreateGlobalParticipant();
        GlobalParticipant globalParticipantParsed = mapper.readValue(
                GlobalParticipantJson, GlobalParticipant.class);
        Struct kafkaStruct = globalParticipant.ToKafkaStruct();

        assertEquals(
                globalParticipantParsed.name,
                kafkaStruct.getString("name"));
        assertEquals(
                globalParticipantParsed.id,
                kafkaStruct.getString("id"));
        assertEquals(
                globalParticipantParsed.imageUrl,
                kafkaStruct.getString("imageUrl"));
        assertEquals(
                globalParticipantParsed.countryCode,
                kafkaStruct.getString("countryCode"));
        assertEquals(
                globalParticipantParsed.hasPublicData,
                kafkaStruct.getBoolean("hasPublicData"));

        Struct statsStruct = kafkaStruct.getStruct("stats");
        assertEquals(
                globalParticipantParsed.stats.followers,
                statsStruct.getInt64("followers"));
        assertEquals(
                globalParticipantParsed.stats.monthlyListeners,
                statsStruct.getInt64("monthlyListeners"));
        assertEquals(
                globalParticipantParsed.stats.popularity,
                statsStruct.getInt64("popularity"));
        assertEquals(
                globalParticipantParsed.stats.views,
                statsStruct.getInt64("views"));

        Struct socialAccountIdsStruct = kafkaStruct.getStruct("socialAccountIds");
        assertEquals(
                globalParticipantParsed.socialAccountIds.spotify,
                socialAccountIdsStruct.getString("spotify"));
        assertEquals(
                globalParticipantParsed.socialAccountIds.appleMusic,
                socialAccountIdsStruct.getString("appleMusic"));
        assertEquals(
                globalParticipantParsed.socialAccountIds.chartmetric,
                socialAccountIdsStruct.getInt64("chartmetric"));
    }
}
