package com.iqser.red.service.peristence.v1.server.integration.tests;

import static com.iqser.red.service.persistence.management.v1.processor.roles.ActionRoles.MANAGE_ACL_PERMISSIONS;
import static com.iqser.red.service.persistence.service.v1.api.external.resource.CustomPermissionMappingResource.PERMISSION_REST_PATH;
import static com.iqser.red.service.persistence.service.v1.api.external.resource.CustomPermissionMappingResource.TARGET_OBJECT_VARIABLE;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

import java.util.ArrayList;
import java.util.List;

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.security.test.context.support.WithMockUser;
import org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;

import com.iqser.red.service.peristence.v1.server.integration.client.CustomPermissionClient;
import com.iqser.red.service.peristence.v1.server.integration.utils.AbstractPersistenceServerServiceTest;
import com.iqser.red.service.persistence.management.v1.processor.acl.RedPermission;
import com.iqser.red.service.persistence.management.v1.processor.acl.custom.constants.CustomPermissionConstants;
import com.iqser.red.service.persistence.service.v1.api.shared.model.permission.CustomPermissionMappingModel;
import com.iqser.red.service.persistence.service.v1.api.shared.model.permission.CustomPermissionModel;

public class CustomPermissionTest extends AbstractPersistenceServerServiceTest {

    @Autowired
    private CustomPermissionClient customPermissionClient;


    @Test
    public void testSaveCustomPermissions() {

        CustomPermissionModel targetViewPermission = new CustomPermissionModel(RedPermission.VIEW_OBJECT.getMask(),
                RedPermission.VIEW_OBJECT.getName(),
                RedPermission.VIEW_OBJECT.getSort(),
                false);
        List<CustomPermissionModel> mappedViewPermissions = new ArrayList<>();
        mappedViewPermissions.add(new CustomPermissionModel(RedPermission.APPROVE.getMask(), RedPermission.APPROVE.getName(), RedPermission.APPROVE.getSort(), true));
        mappedViewPermissions.add(new CustomPermissionModel(RedPermission.REVIEW.getMask(), RedPermission.REVIEW.getName(), RedPermission.REVIEW.getSort(), true));

        CustomPermissionModel targetAccessPermission = new CustomPermissionModel(RedPermission.ACCESS_OBJECT.getMask(),
                RedPermission.ACCESS_OBJECT.getName(),
                RedPermission.ACCESS_OBJECT.getSort(),
                false);
        List<CustomPermissionModel> mappedAccessPermissions = new ArrayList<>();
        mappedAccessPermissions.add(new CustomPermissionModel(RedPermission.APPROVE.getMask(), RedPermission.APPROVE.getName(), RedPermission.APPROVE.getSort(), true));
        mappedAccessPermissions.add(new CustomPermissionModel(RedPermission.REVIEW.getMask(), RedPermission.REVIEW.getName(), RedPermission.REVIEW.getSort(), true));
        mappedAccessPermissions.add(new CustomPermissionModel(CustomPermissionConstants.EVERYONE_ELSE.getMask(),
                CustomPermissionConstants.EVERYONE_ELSE.getName(),
                CustomPermissionConstants.EVERYONE_ELSE.getSort(),
                true));

        List<CustomPermissionMappingModel> customPermissionMappingModels = new ArrayList<>();

        customPermissionMappingModels.add(new CustomPermissionMappingModel(targetViewPermission, mappedViewPermissions));
        customPermissionMappingModels.add(new CustomPermissionMappingModel(targetAccessPermission, mappedAccessPermissions));

        customPermissionClient.saveCustomPermissionMappings("Dossier", customPermissionMappingModels);

    }

}
