package com.iqser.red.service.persistence.management.v1.processor.service.job;

import java.time.DayOfWeek;
import java.time.OffsetDateTime;
import java.time.temporal.ChronoUnit;

import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.springframework.stereotype.Service;

import com.iqser.red.service.persistence.management.v1.processor.service.persistence.NotificationEmailService;
import com.iqser.red.service.persistence.management.v1.processor.service.persistence.NotificationPersistenceService;
import com.iqser.red.service.persistence.management.v1.processor.service.persistence.NotificationPreferencesPersistenceService;
import com.iqser.red.service.persistence.management.v1.processor.utils.TenantUtils;
import com.iqser.red.service.persistence.service.v1.api.shared.model.notification.EmailNotificationType;
import com.knecon.fforesight.tenantcommons.TenantContext;
import com.knecon.fforesight.tenantcommons.TenantProvider;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@RequiredArgsConstructor
@Service
public class SendNotificationEmailJob implements Job {

    private final NotificationEmailService notificationEmailService;
    private final NotificationPersistenceService notificationPersistenceService;
    private final NotificationPreferencesPersistenceService notificationPreferencesPersistenceService;
    private final TenantProvider tenantProvider;


    @Override
    public void execute(JobExecutionContext jobExecutionContext) {

        tenantProvider.getTenants().forEach(tenant -> {

            if(!TenantUtils.isTenantReadyForPersistence(tenant)){
                return;
            }

            TenantContext.setTenantId(tenant.getTenantId());

            var allConfiguredPreferences = notificationPreferencesPersistenceService.findAll();
            allConfiguredPreferences.forEach(preference -> {
                if (preference.isEmailNotificationsEnabled() && preference.getEmailNotificationType() == EmailNotificationType.WEEKLY_SUMMARY || preference.getEmailNotificationType() == EmailNotificationType.DAILY_SUMMARY) {

                    var now = OffsetDateTime.now().truncatedTo(ChronoUnit.HOURS);

                    // Weekly summary is sent monday night
                    if (now.getDayOfWeek() == DayOfWeek.MONDAY && preference.getEmailNotificationType() == EmailNotificationType.WEEKLY_SUMMARY) {
                        var from = OffsetDateTime.now().minusDays(7).truncatedTo(ChronoUnit.HOURS).withHour(0);
                        var to = OffsetDateTime.now().truncatedTo(ChronoUnit.HOURS).withHour(0);
                        var notifications = notificationPersistenceService.getNotificationsForEmailSummary(preference.getUserId(), from, to);

                        notificationEmailService.sendNotificationEmail(preference.getUserId(), EmailNotificationType.WEEKLY_SUMMARY, notifications);
                    } else {
                        var from = OffsetDateTime.now().minusDays(1).withHour(0).truncatedTo(ChronoUnit.HOURS);
                        var to = OffsetDateTime.now().minusDays(1).withHour(23).truncatedTo(ChronoUnit.HOURS);
                        var notifications = notificationPersistenceService.getNotificationsForEmailSummary(preference.getUserId(), from, to);

                        notificationEmailService.sendNotificationEmail(preference.getUserId(), EmailNotificationType.DAILY_SUMMARY, notifications);

                    }

                }
            });
        });
    }

}
