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

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

import java.util.List;
import java.util.Set;

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.iqser.red.service.persistence.management.v1.processor.acl.custom.service.CustomPermissionService;
import com.iqser.red.service.persistence.service.v1.api.external.resource.CustomPermissionMappingResource;
import com.iqser.red.service.persistence.service.v1.api.shared.model.permission.CustomPermissionMappingModel;

import lombok.RequiredArgsConstructor;

@RestController
@RequiredArgsConstructor
public class CustomPermissionMappingController implements CustomPermissionMappingResource {

    private final CustomPermissionService customPermissionService;


    @Override
    @PreAuthorize("hasAuthority('" + MANAGE_ACL_PERMISSIONS + "')")
    public List<CustomPermissionMappingModel> getCustomPermissionMappings(@PathVariable(TARGET_OBJECT_NAME) String targetObject) {

        return customPermissionService.getCustomPermissionMappings(targetObject);
    }


    @Override
    @PreAuthorize("hasAuthority('" + MANAGE_ACL_PERMISSIONS + "')")
    public void saveCustomPermissionMappings(@PathVariable(TARGET_OBJECT_NAME) String targetObject, @RequestBody List<CustomPermissionMappingModel> customPermissionMappingModels) {

        customPermissionService.saveCustomPermissionMappings(targetObject, customPermissionMappingModels);
    }


    @Override
    @PreAuthorize("hasAuthority('" + MANAGE_ACL_PERMISSIONS + "')")
    public List<CustomPermissionMappingModel> getValidMapping(@PathVariable(TARGET_OBJECT_NAME) String targetObject) {

        return customPermissionService.getExistingPermissions(targetObject);
    }


    @Override
    @PreAuthorize("hasAuthority('" + MANAGE_ACL_PERMISSIONS + "')")
    public Set<String> getAllSupportedTargetObjects() {

        return customPermissionService.getAllSupportedTargetObjects();
    }


    @Override
    @PreAuthorize("hasAuthority('" + MANAGE_ACL_PERMISSIONS + "')")
    public void syncAllCustomPermissions() {

        customPermissionService.syncAllCustomPermissions();
    }

}
