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

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

import org.quartz.DisallowConcurrentExecution;
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.entity.dossier.FileEntity;
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.FileDeletionService;
import com.iqser.red.service.persistence.management.v1.processor.service.persistence.repository.DossierRepository;
import com.iqser.red.service.persistence.management.v1.processor.service.persistence.repository.FileRepository;
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
@DisallowConcurrentExecution
public class SoftDeletedFilesCleanupJob implements Job {

    private final FileRepository fileRepository;
    private final DossierRepository dossierRepository;
    private final DossierService dossierService;
    private final FileDeletionService fileDeletionService;
    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 applicationConfigurationEntity = applicationConfigService.getApplicationConfig();
                    var now = OffsetDateTime.now();
                    OffsetDateTime deletionThreshold = now.minusHours(applicationConfigurationEntity.getSoftDeleteCleanupTime());

                    List<DossierEntity> dossiers = dossierRepository.findDossiersSoftDeletedBefore(deletionThreshold);
                    for (DossierEntity dossierEntity : dossiers) {
                        dossierService.hardDeleteDossier(dossierEntity.getId());
                        log.info("Hard deleted dossier with dossier id {} ", dossierEntity.getId());

                    }

                    List<FileEntity> files = fileRepository.findFilesSoftDeletedBefore(deletionThreshold);
                    for (var file : files) {

                        fileDeletionService.hardDeleteFile(file.getDossierId(), file.getId());
                        log.info("Hard deleted file with dossier id {} and file id {}", file.getDossierId(), file.getId());

                    }
                });
    }

}
