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

import com.iqser.red.service.persistence.management.v1.processor.entity.annotations.AnnotationEntityId;
import com.iqser.red.service.persistence.management.v1.processor.entity.migration.SaasMigrationStatusEntity;
import com.iqser.red.service.persistence.management.v1.processor.exception.NotFoundException;
import com.iqser.red.service.persistence.management.v1.processor.service.persistence.repository.SaasMigrationStatusRepository;
import com.iqser.red.service.persistence.service.v1.api.shared.model.dossiertemplate.dossier.file.SaasMigrationStatus;

import jakarta.transaction.Transactional;
import lombok.RequiredArgsConstructor;

import org.springframework.stereotype.Service;

import java.util.List;

@Service
@RequiredArgsConstructor
public class SaasMigrationStatusPersistenceService {

    private final SaasMigrationStatusRepository saasMigrationStatusRepository;


    public List<SaasMigrationStatusEntity> findAllByStatus(SaasMigrationStatus status) {

        return saasMigrationStatusRepository.findAllByStatus(status);
    }


    public SaasMigrationStatusEntity findById(String fileId) {

        var migrationStatusOptional = saasMigrationStatusRepository.findById(fileId);
        if (migrationStatusOptional.isPresent()) {
            return migrationStatusOptional.get();
        }
        throw new NotFoundException("No migration entry found for fileId" + fileId);
    }


    public boolean isMigrating(String fileId) {

        var migrationStatusOptional = saasMigrationStatusRepository.findById(fileId);
        return migrationStatusOptional.isPresent() && migrationStatusOptional.get().getStatus() != SaasMigrationStatus.FINISHED;
    }

    public boolean migrationFinishedForTenant() {

        return saasMigrationStatusRepository.findAllWhereStatusNotFinishedAndNotError() == 0;
    }


    @Transactional
    public void createMigrationRequiredStatus(String dossierId, String fileId) {

        saasMigrationStatusRepository.save(SaasMigrationStatusEntity.builder().fileId(fileId).dossierId(dossierId).status(SaasMigrationStatus.MIGRATION_REQUIRED).build());
    }


    @Transactional
    public void updateStatus(String fileId, SaasMigrationStatus status) {

        saasMigrationStatusRepository.updateStatus(fileId, status);
    }


    @Transactional
    public void updateErrorStatus(String fileId, String errorCause) {

        saasMigrationStatusRepository.updateErrorStatus(fileId, SaasMigrationStatus.ERROR, errorCause);
    }


    @Transactional
    public void updateErrorCounter(String fileId, Integer processingErrorCounter, String errorCause) {

        saasMigrationStatusRepository.updateErrorCounter(fileId, processingErrorCounter, errorCause);
    }


    public int countByStatus(SaasMigrationStatus status) {

        return saasMigrationStatusRepository.countByStatus(status);
    }


    public int countAll() {

        return saasMigrationStatusRepository.countAll();
    }


    public List<SaasMigrationStatusEntity> findAll() {

        return saasMigrationStatusRepository.findAll();
    }

}
