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

import static com.iqser.red.service.persistence.management.v1.processor.roles.ActionRoles.READ_LEGAL_BASIS;
import static com.iqser.red.service.persistence.management.v1.processor.roles.ActionRoles.WRITE_LEGAL_BASIS;

import java.util.List;

import jakarta.transaction.Transactional;

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

import com.knecon.fforesight.keycloakcommons.security.KeycloakSecurity;
import com.iqser.red.service.persistence.management.v1.processor.service.persistence.AuditPersistenceService;
import com.iqser.red.service.persistence.management.v1.processor.service.persistence.LegalBasisMappingPersistenceService;
import com.knecon.fforesight.databasetenantcommons.providers.utils.MagicConverter;
import com.iqser.red.service.persistence.service.v1.api.external.resource.LegalBasisMappingResource;
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.iqser.red.service.persistence.service.v1.api.shared.model.dossiertemplate.legalbasis.LegalBasis;

import lombok.RequiredArgsConstructor;

@RestController
@RequiredArgsConstructor
public class LegalBasisMappingController implements LegalBasisMappingResource {

    private final LegalBasisMappingPersistenceService legalBasisMappingPersistenceService;
    private final AuditPersistenceService auditPersistenceService;


    @Override
    @PreAuthorize("hasAuthority('" + WRITE_LEGAL_BASIS + "')")
    public void deleteLegalBasis(@PathVariable(DOSSIER_TEMPLATE_PARAMETER_NAME) String dossierTemplateId, @RequestBody List<String> legalBasisNames) {

        legalBasisMappingPersistenceService.deleteLegalBasis(dossierTemplateId, legalBasisNames);
        auditPersistenceService.audit(AuditRequest.builder()
                .userId(KeycloakSecurity.getUserId())
                .objectId(dossierTemplateId)
                .category(AuditCategory.DOSSIER_TEMPLATE.name())
                .message("Legal basis mapping has been changed.")
                .build());
    }


    @Override
    @PreAuthorize("hasAuthority('" + WRITE_LEGAL_BASIS + "')")
    public void addOrUpdateLegalBasis(@PathVariable(DOSSIER_TEMPLATE_PARAMETER_NAME) String dossierTemplateId, @RequestBody LegalBasis legalBasis) {

        legalBasisMappingPersistenceService.addOrUpdateLegalBasis(dossierTemplateId, legalBasis);
        auditPersistenceService.audit(AuditRequest.builder()
                .userId(KeycloakSecurity.getUserId())
                .objectId(dossierTemplateId)
                .category(AuditCategory.DOSSIER_TEMPLATE.name())
                .message("Legal basis mapping has been changed.")
                .build());
    }


    @PreAuthorize("hasAuthority('" + WRITE_LEGAL_BASIS + "')")
    public void setLegalBasisMapping(@RequestBody List<LegalBasis> legalBasisMapping, @PathVariable(DOSSIER_TEMPLATE_PARAMETER_NAME) String dossierTemplateId) {

        legalBasisMappingPersistenceService.setLegalBasisMapping(dossierTemplateId, legalBasisMapping);
        auditPersistenceService.audit(AuditRequest.builder()
                .userId(KeycloakSecurity.getUserId())
                .objectId(dossierTemplateId)
                .category(AuditCategory.DOSSIER_TEMPLATE.name())
                .message("Legal basis mapping has been changed.")
                .build());
    }

    @Transactional
    @PreAuthorize("hasAuthority('" + READ_LEGAL_BASIS + "')")
    public List<LegalBasis> getLegalBasisMapping(@PathVariable(DOSSIER_TEMPLATE_PARAMETER_NAME) String dossierTemplateId) {

        return MagicConverter.convert(legalBasisMappingPersistenceService.getLegalBasisMapping(dossierTemplateId), LegalBasis.class);
    }

}
