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

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.iqser.red.service.persistence.management.v1.processor.migration.Migration;
import com.iqser.red.service.persistence.management.v1.processor.migration.migrations.model.simplifiedtext.Text;
import com.iqser.red.service.persistence.management.v1.processor.service.persistence.DossierPersistenceService;
import com.iqser.red.service.persistence.management.v1.processor.service.persistence.FileStatusPersistenceService;
import com.iqser.red.service.persistence.management.v1.processor.utils.StorageIdUtils;
import com.iqser.red.service.persistence.service.v1.api.shared.model.dossiertemplate.dossier.file.FileType;
import com.iqser.red.storage.commons.exception.StorageObjectDoesNotExist;
import com.iqser.red.storage.commons.service.StorageService;
import com.knecon.fforesight.tenantcommons.TenantContext;

import lombok.Setter;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@Setter
@Service
public class SimplifiedTextMigration12 extends Migration {

    private static final String NAME = "Add simplified text file if missing";
    private static final long VERSION = 12;

    @Autowired
    private DossierPersistenceService dossierPersistenceService;

    @Autowired
    private FileStatusPersistenceService fileStatusPersistenceService;

    @Autowired
    private StorageService storageService;


    public SimplifiedTextMigration12() {

        super(NAME, VERSION);
    }


    @Override
    protected void migrate() {

        var dossiers = dossierPersistenceService.findAllDossiers();
        dossiers.forEach(dossier -> {
            if (dossier.getHardDeletedTime() == null) {
                var files = fileStatusPersistenceService.getStatusesForDossier(dossier.getId());
                log.info("Start migration of dossier {}", dossier.getId());
                files.forEach(file -> {
                    if (file.getHardDeletedTime() == null) {
                        log.info("Start migration of file {}", file.getId());
                        migrateFile(dossier.getId(), file.getId());
                        log.info("Finished migration of file {}", file.getId());
                    }
                });
                log.info("Finished migration of dossier {}", dossier.getId());
            }
        });
    }


    @SneakyThrows
    public void migrateFile(String dossierId, String fileId) {

        try {

            if (!storageService.objectExists(TenantContext.getTenantId(), StorageIdUtils.getStorageId(dossierId, fileId, FileType.SIMPLIFIED_TEXT))) {
                var text = storageService.readJSONObject(TenantContext.getTenantId(), StorageIdUtils.getStorageId(dossierId, fileId, FileType.TEXT), Text.class);
                storageService.storeJSONObject(TenantContext.getTenantId(), StorageIdUtils.getStorageId(dossierId, fileId, FileType.SIMPLIFIED_TEXT), text);
            }
        } catch (StorageObjectDoesNotExist e) {
            log.warn("Text not found for dossier {} and file {}, ignoring....", dossierId, fileId);
        }
    }

}





