package com.writeoff.module.system.controller;

import com.writeoff.common.api.ApiResponse;
import com.writeoff.common.api.PageResult;
import com.writeoff.module.system.dto.BindRoleMenusRequest;
import com.writeoff.module.system.dto.BindRolePermissionsRequest;
import com.writeoff.module.system.dto.CreateRoleRequest;
import com.writeoff.module.system.dto.UpdateRoleRequest;
import com.writeoff.module.system.model.RoleInfo;
import com.writeoff.module.system.service.PlatformIamService;
import com.writeoff.module.system.service.PlatformMenuService;
import com.writeoff.security.DataScopeType;
import com.writeoff.security.PermissionDomain;
import com.writeoff.security.RequirePermission;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

@RestController
@RequestMapping("/api/platform/roles")
public class PlatformRoleController {
    private final PlatformIamService platformIamService;
    private final PlatformMenuService platformMenuService;

    public PlatformRoleController(PlatformIamService platformIamService, PlatformMenuService platformMenuService) {
        this.platformIamService = platformIamService;
        this.platformMenuService = platformMenuService;
    }

    @GetMapping
    @RequirePermission(value = "platform.role.read", domain = PermissionDomain.PLATFORM, dataScope = DataScopeType.GLOBAL_READONLY, auditAction = "PLATFORM_ROLE_LIST")
    public ApiResponse<PageResult<RoleInfo>> list() {
        return ApiResponse.success(platformIamService.listRoles());
    }

    @PostMapping
    @RequirePermission(value = "platform.role.manage", domain = PermissionDomain.PLATFORM, dataScope = DataScopeType.GLOBAL_READONLY, auditAction = "PLATFORM_ROLE_CREATE")
    public ApiResponse<RoleInfo> create(@RequestBody @Valid CreateRoleRequest request) {
        return ApiResponse.success(platformIamService.createRole(request));
    }

    @PutMapping("/{id}")
    @RequirePermission(value = "platform.role.manage", domain = PermissionDomain.PLATFORM, dataScope = DataScopeType.GLOBAL_READONLY, auditAction = "PLATFORM_ROLE_UPDATE")
    public ApiResponse<RoleInfo> update(@PathVariable("id") Long id, @RequestBody @Valid UpdateRoleRequest request) {
        return ApiResponse.success(platformIamService.updateRole(id, request));
    }

    @PostMapping("/{id}/enable")
    @RequirePermission(value = "platform.role.manage", domain = PermissionDomain.PLATFORM, dataScope = DataScopeType.GLOBAL_READONLY, auditAction = "PLATFORM_ROLE_ENABLE")
    public ApiResponse<String> enable(@PathVariable("id") Long id) {
        platformIamService.enableRole(id);
        return ApiResponse.success("OK");
    }

    @PostMapping("/{id}/disable")
    @RequirePermission(value = "platform.role.manage", domain = PermissionDomain.PLATFORM, dataScope = DataScopeType.GLOBAL_READONLY, auditAction = "PLATFORM_ROLE_DISABLE")
    public ApiResponse<String> disable(@PathVariable("id") Long id) {
        platformIamService.disableRole(id);
        return ApiResponse.success("OK");
    }

    @GetMapping("/{id}/permissions")
    @RequirePermission(value = "platform.role.read", domain = PermissionDomain.PLATFORM, dataScope = DataScopeType.GLOBAL_READONLY, auditAction = "PLATFORM_ROLE_PERMISSIONS")
    public ApiResponse<java.util.List<Long>> getRolePermissions(@PathVariable("id") Long id) {
        return ApiResponse.success(platformIamService.getRolePermissionIds(id));
    }

    @PostMapping("/{id}/permissions")
    @RequirePermission(value = "platform.role.manage", domain = PermissionDomain.PLATFORM, dataScope = DataScopeType.GLOBAL_READONLY, auditAction = "PLATFORM_ROLE_BIND_PERMISSIONS")
    public ApiResponse<String> bindPermissions(@PathVariable("id") Long id, @RequestBody @Valid BindRolePermissionsRequest request) {
        platformIamService.bindRolePermissions(id, request);
        return ApiResponse.success("OK");
    }

    @GetMapping("/{id}/menus")
    @RequirePermission(value = "platform.role.read", domain = PermissionDomain.PLATFORM, dataScope = DataScopeType.GLOBAL_READONLY, auditAction = "PLATFORM_ROLE_MENUS")
    public ApiResponse<java.util.List<Long>> getRoleMenus(@PathVariable("id") Long id) {
        return ApiResponse.success(platformMenuService.getRoleMenuIds(id));
    }

    @PostMapping("/{id}/menus")
    @RequirePermission(value = "platform.role.manage", domain = PermissionDomain.PLATFORM, dataScope = DataScopeType.GLOBAL_READONLY, auditAction = "PLATFORM_ROLE_BIND_MENUS")
    public ApiResponse<String> bindMenus(@PathVariable("id") Long id, @RequestBody @Valid BindRoleMenusRequest request) {
        platformMenuService.bindRoleMenus(id, request);
        return ApiResponse.success("OK");
    }
}
