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

import static com.iqser.red.service.persistence.management.v1.processor.cache.PersistenceServiceExternalApiCacheConfiguration.VALID_FILE_FORMATS_CACHE;
import static com.iqser.red.service.persistence.management.v1.processor.service.LicenseUtilityService.ALL_FILE_FORMATS;
import static com.iqser.red.service.persistence.management.v1.processor.service.LicenseUtilityService.FILE_FORMATS_EXCLUDING_MS_OFFICE;
import static com.iqser.red.service.persistence.management.v1.processor.service.LicenseUtilityService.LICENSE_OBJECT_ID;
import static com.iqser.red.service.persistence.management.v1.processor.service.LicenseUtilityService.SUPPORT_MS_OFFICE_FORMATS_FEATURE;

import java.util.List;
import java.util.Objects;
import java.util.Optional;

import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import com.iqser.red.service.persistence.service.v1.api.shared.model.license.Feature;
import com.iqser.red.service.persistence.service.v1.api.shared.model.license.License;
import com.iqser.red.service.persistence.service.v1.api.shared.model.license.RedactionLicenseModel;
import com.iqser.red.storage.commons.service.StorageService;

import lombok.AccessLevel;
import lombok.RequiredArgsConstructor;
import lombok.experimental.FieldDefaults;

@Service
@RequiredArgsConstructor
@FieldDefaults(level = AccessLevel.PRIVATE)
public class FileFormatValidationService {

    final StorageService storageService;

    final LicenseUtilityService licenseUtilityService;


    public List<String> getAllFileFormats() {

        return ALL_FILE_FORMATS;
    }


    @Cacheable(value = VALID_FILE_FORMATS_CACHE, key = "#tenantId")
    public List<String> getValidFileFormatsForTenant(String tenantId) {

        boolean supportMSOfficeFormats = false;

        Optional<License> optionalLicense = getActiveLicense(tenantId);
        if (optionalLicense.isPresent()) {
            Optional<Feature> optionalSupportMSOfficeFormatsFeature = optionalLicense.get().getFeatures()
                    .stream()
                    .filter(f -> f.getName().equals(SUPPORT_MS_OFFICE_FORMATS_FEATURE))
                    .findFirst();

            supportMSOfficeFormats = optionalSupportMSOfficeFormatsFeature.isPresent() ? (Boolean) optionalSupportMSOfficeFormatsFeature.get().getValue() : false;
        }

        return supportMSOfficeFormats ? ALL_FILE_FORMATS : FILE_FORMATS_EXCLUDING_MS_OFFICE;

    }

    @CacheEvict(value = VALID_FILE_FORMATS_CACHE, key = "#tenantId")
    public void evictValidFileFormatsForTenant(String tenantId) {
        // empty
    }


    private Optional<License> getActiveLicense(String tenantId) {

        var redactionLicenseModel = getLicenseForTenant(tenantId);
        return redactionLicenseModel.getLicenses()
                .stream()
                .filter(rlm -> Objects.equals(rlm.getId(), redactionLicenseModel.getActiveLicense()))
                .findFirst();

    }


    private RedactionLicenseModel getLicenseForTenant(String tenantId) {

        if (storageService.objectExists(tenantId, LICENSE_OBJECT_ID)) {
            try {
                return storageService.readJSONObject(tenantId, LICENSE_OBJECT_ID, RedactionLicenseModel.class);
            } catch (Exception e) {
                return licenseUtilityService.generateDemoLicense();
            }
        }
        return licenseUtilityService.generateDemoLicense();
    }

}