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

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

import java.util.ArrayList;
import java.util.List;

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

import com.iqser.red.service.peristence.v1.server.integration.client.NotificationPreferencesClient;
import com.iqser.red.service.peristence.v1.server.integration.utils.AbstractPersistenceServerServiceTest;
import com.iqser.red.service.persistence.service.v1.api.shared.model.notification.EmailNotificationType;
import com.iqser.red.service.persistence.service.v1.api.shared.model.notification.NotificationPreferences;

import feign.FeignException;

public class NotificationPreferencesTest extends AbstractPersistenceServerServiceTest {

    private static final String USER_ID = "1234";
    private static final String DOSSIER_YOU_OWN_NOTIFICATION_1 = "Dossier status changes";
    private static final String DOSSIER_YOU_OWN_NOTIFICATION_2 = "Requests to join dossier";
    private static final String DOSSIER_YOU_OWN_NOTIFICATION_3 = "Document status changes";
    private static final String DOSSIER_YOU_OWN_NOTIFICATION_4 = "Document is sent for approval";
    List<String> inAppNotifications = new ArrayList<>();
    List<String> emailNotifications = new ArrayList<>();
    @Autowired
    private NotificationPreferencesClient notificationPreferencesClient;
    private NotificationPreferences notificationPreferences;


    @BeforeEach
    public void setupData() {

        inAppNotifications.add(DOSSIER_YOU_OWN_NOTIFICATION_1);
        inAppNotifications.add(DOSSIER_YOU_OWN_NOTIFICATION_2);
        inAppNotifications.add(DOSSIER_YOU_OWN_NOTIFICATION_3);
        emailNotifications.add(DOSSIER_YOU_OWN_NOTIFICATION_1);
        emailNotifications.add(DOSSIER_YOU_OWN_NOTIFICATION_2);
        emailNotifications.add(DOSSIER_YOU_OWN_NOTIFICATION_4);
        notificationPreferences = new NotificationPreferences(true, true, EmailNotificationType.DAILY, emailNotifications, inAppNotifications);
    }


    @Test
    public void testNotificationPreferences() {
        // add notification
        notificationPreferencesClient.setNotificationPreferences(notificationPreferences);

        NotificationPreferences notificationReceived = notificationPreferencesClient.getNotificationPreferences();

        assertThat(notificationReceived.getEmailNotificationType()).isEqualTo(EmailNotificationType.DAILY);
        assertThat(notificationReceived.isEmailNotificationsEnabled()).isTrue();
        assertThat(notificationReceived.isInAppNotificationsEnabled()).isTrue();
        assertThat(notificationReceived.getInAppNotifications().size()).isEqualTo(notificationPreferences.getInAppNotifications().size());
        assertThat(notificationReceived.getEmailNotifications().size()).isEqualTo(notificationPreferences.getEmailNotifications().size());

        // update notification
        notificationPreferences.getInAppNotifications().add(DOSSIER_YOU_OWN_NOTIFICATION_4);
        notificationPreferences.setEmailNotificationsEnabled(false);

        notificationPreferencesClient.setNotificationPreferences(notificationPreferences);

        notificationReceived = notificationPreferencesClient.getNotificationPreferences();

        assertThat(notificationReceived.getEmailNotificationType()).isEqualTo(EmailNotificationType.DAILY);
        assertThat(notificationReceived.isEmailNotificationsEnabled()).isFalse();
        assertThat(notificationReceived.isInAppNotificationsEnabled()).isTrue();
        assertThat(notificationReceived.getInAppNotifications().size()).isEqualTo(notificationPreferences.getInAppNotifications().size());
        assertThat(notificationReceived.getEmailNotifications().size()).isEqualTo(notificationPreferences.getEmailNotifications().size());

        // delete notification
        notificationPreferencesClient.deleteNotificationPreferences();
        try {
            notificationReceived = notificationPreferencesClient.getNotificationPreferences();
        } catch (FeignException e) {
            assertThat(e.status()).isEqualTo(404);
        }
    }

}
