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

import java.io.ByteArrayInputStream;

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.service.FileManagementStorageService;
import com.iqser.red.service.persistence.management.v1.processor.service.FileStatusService;
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.service.v1.api.shared.model.dossiertemplate.dossier.file.FileType;

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

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

    private static final String NAME = "Migrate Highlights";
    private static final long VERSION = 3;

    @Autowired
    private DossierPersistenceService dossierPersistenceService;

    @Autowired
    private FileStatusPersistenceService fileStatusPersistenceService;

    @Autowired
    private FileManagementStorageService fileManagementStorageService;

    @Autowired
    private FileStatusService fileStatusService;


    public MigrateHighlights3() {

        super(NAME, VERSION);
    }


    @Override
    protected void migrate() {

        var dossiers = dossierPersistenceService.findAllDossiers();
        dossiers.forEach(dossier -> {
            var files = fileStatusPersistenceService.getStatusesForDossier(dossier.getId());
            files.forEach(file -> {
                if (file.getHardDeletedTime() == null) {

                    try {
                        if (fileManagementStorageService.objectExists(dossier.getId(), file.getId(), FileType.ORIGIN)) {
                            var untouchedExists = fileManagementStorageService.objectExists(dossier.getId(), file.getId(), FileType.UNTOUCHED);

                            if (!untouchedExists) {
                                var originExists = fileManagementStorageService.objectExists(dossier.getId(), file.getId(), FileType.ORIGIN);

                                if (!originExists) {
                                    log.warn("Invalid file {} / {} Neither untouched nor origin files exists!", dossier.getId(), file.getId());
                                    return;
                                }

                                fileManagementStorageService.storeObject(dossier.getId(),
                                        file.getId(),
                                        FileType.UNTOUCHED,
                                        new ByteArrayInputStream(fileManagementStorageService.getStoredObjectBytes(dossier.getId(), file.getId(), FileType.ORIGIN)));

                            }

                            fileStatusService.addToPreprocessingQueue(file.getId(), file.getDossierId(), file.getFilename());

                        } else {
                            log.warn("Invalid file: {} in dossier: {}. File Data ( PDF ) does not exist", file.getId(), file.getDossierId());
                        }
                    } catch (Exception e) {
                        log.warn("Failed to extract text highlights for document: {}", file.getId());
                    }
                }
            });
        });
    }

}
