package com.iqser.red.persistence.service.v1.external.api.impl.controller;

import static com.iqser.red.service.persistence.management.v1.processor.roles.ActionRoles.READ_APP_CONFIG;
import static com.iqser.red.service.persistence.management.v1.processor.roles.ActionRoles.WRITE_APP_CONFIG;

import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import com.iqser.red.service.persistence.management.v1.processor.entity.configuration.ApplicationConfigurationEntity;
import com.iqser.red.service.persistence.management.v1.processor.service.ApplicationConfigService;
import com.iqser.red.service.persistence.management.v1.processor.service.persistence.AuditPersistenceService;
import com.iqser.red.service.persistence.service.v1.api.shared.model.AuditCategory;
import com.iqser.red.service.persistence.service.v1.api.shared.model.audit.AuditRequest;
import com.knecon.fforesight.databasetenantcommons.providers.utils.MagicConverter;
import com.iqser.red.service.persistence.service.v1.api.external.resource.ApplicationConfigurationResource;
import com.iqser.red.service.persistence.service.v1.api.shared.model.dossiertemplate.configuration.ApplicationConfig;
import com.knecon.fforesight.keycloakcommons.security.KeycloakSecurity;

import jakarta.persistence.Column;
import jakarta.validation.Valid;
import lombok.Builder;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@RestController
@RequiredArgsConstructor
public class ApplicationConfigurationController implements ApplicationConfigurationResource {

    private final ApplicationConfigService applicationConfigService;
    private final AuditPersistenceService auditPersistenceService;


    @Override
    @PreAuthorize("hasAuthority('" + WRITE_APP_CONFIG + "')")
    public ApplicationConfig createOrUpdateAppConfig(@Valid @RequestBody ApplicationConfig appConfig) {

        var result = MagicConverter.convert(applicationConfigService.saveApplicationConfiguration(convert(appConfig)),
                                      ApplicationConfig.class);

        auditPersistenceService.audit(AuditRequest.builder()
                                              .userId(KeycloakSecurity.getUserId())
                                              .objectId("ApplicationConfig")
                                              .category(AuditCategory.SETTINGS.name())
                                              .message("Application config has been changed.")
                                              .build());

        return result;
    }


    @Override
    @PreAuthorize("hasAuthority('" + READ_APP_CONFIG + "')")
    public ApplicationConfig getCurrentApplicationConfig() {

        return MagicConverter.convert(applicationConfigService.getApplicationConfig(), ApplicationConfig.class);
    }


    private ApplicationConfigurationEntity convert(ApplicationConfig appConfig){
        var entity = ApplicationConfigurationEntity.builder()
                .downloadCleanupDownloadFilesHours(appConfig.getDownloadCleanupDownloadFilesHours())
                .downloadCleanupNotDownloadFilesHours(appConfig.getDownloadCleanupNotDownloadFilesHours())
                .softDeleteCleanupTime(appConfig.getSoftDeleteCleanupTime())
                .build();

        if(appConfig.getHardDeleteCleanupRetryTime() != null){
            entity.setHardDeleteCleanupRetryTime(appConfig.getHardDeleteCleanupRetryTime());
        }
        return entity;
    }

}
