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

import java.time.OffsetDateTime;
import java.util.List;

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

import com.iqser.red.service.persistence.management.v1.processor.entity.dossier.DossierEntity;
import com.iqser.red.service.persistence.management.v1.processor.service.ApplicationConfigService;
import com.iqser.red.service.persistence.management.v1.processor.service.DossierService;
import com.iqser.red.service.persistence.management.v1.processor.service.FileService;
import com.iqser.red.service.persistence.management.v1.processor.service.FileStatusService;
import com.iqser.red.service.persistence.management.v1.processor.utils.TenantUtils;
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 DeletedFilesCleanupJob implements Job {

    private final DossierService dossierService;
    private final FileStatusService fileStatusService;
    private final FileService fileService;
    private final ApplicationConfigService applicationConfigService;
    private final TenantProvider tenantProvider;


    @Override
    public void execute(JobExecutionContext jobExecutionContext) {

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

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

            TenantContext.setTenantId(tenant.getTenantId());

            var now = OffsetDateTime.now();
            List<DossierEntity> dossiers = dossierService.getAllDossiers();
            var applicationConfigurationEntity = applicationConfigService.getApplicationConfig();

            for (DossierEntity dossierEntity : dossiers) {
                if (dossierEntity.getSoftDeletedTime() != null && dossierEntity.getHardDeletedTime() == null) {
                    if (dossierEntity.getSoftDeletedTime().isBefore(now.minusHours(applicationConfigurationEntity.getSoftDeleteCleanupTime()))) {
                        dossierService.hardDeleteDossier(dossierEntity.getId());
                        log.info("Hard deleted dossier with dossier id {} ", dossierEntity.getId());
                    }
                } else {
                    var files = fileStatusService.getDossierStatus(dossierEntity.getId());
                    for (var file : files) {
                        if (file.getHardDeletedTime() == null && file.getDeleted() != null && file.getDeleted()
                                .isBefore(now.minusHours(applicationConfigurationEntity.getSoftDeleteCleanupTime()))) {
                            fileService.hardDeleteFile(dossierEntity.getId(), file.getId());
                            fileStatusService.setFileStatusHardDeleted(file.getId());
                            log.info("Hard deleted file with dossier id {} and file id {}", dossierEntity.getId(), file.getId());
                        }
                    }
                }
            }

        });
    }

}
