package com.writeoff.module.system.controller;

import com.writeoff.common.api.ApiResponse;
import com.writeoff.common.api.PageResult;
import com.writeoff.module.system.dto.BindRolePermissionsRequest;
import com.writeoff.module.system.dto.BindRoleMenusRequest;
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.MenuService;
import com.writeoff.module.system.service.SystemUserService;
import com.writeoff.security.DataScopeType;
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.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

@RestController
@RequestMapping("/api/roles")
public class RoleController {
    private final SystemUserService systemUserService;
    private final MenuService menuService;

    public RoleController(SystemUserService systemUserService, MenuService menuService) {
        this.systemUserService = systemUserService;
        this.menuService = menuService;
    }

    @GetMapping
    @RequirePermission(value = "role.read", dataScope = DataScopeType.TENANT, auditAction = "ROLE_LIST")
    public ApiResponse<PageResult<RoleInfo>> list(
            @RequestParam(value = "pageNo", defaultValue = "1") int pageNo,
            @RequestParam(value = "pageSize", defaultValue = "20") int pageSize) {
        return ApiResponse.success(systemUserService.listRoles(pageNo, pageSize));
    }

    @PostMapping
    @RequirePermission(value = "role.create", dataScope = DataScopeType.TENANT, auditAction = "ROLE_CREATE")
    public ApiResponse<RoleInfo> create(@RequestBody @Valid CreateRoleRequest request) {
        return ApiResponse.success(systemUserService.createRole(request));
    }

    @PutMapping("/{id}")
    @RequirePermission(value = "role.update", dataScope = DataScopeType.TENANT, auditAction = "ROLE_UPDATE")
    public ApiResponse<RoleInfo> update(@PathVariable("id") Long id, @RequestBody @Valid UpdateRoleRequest request) {
        return ApiResponse.success(systemUserService.updateRole(id, request));
    }

    @PostMapping("/{id}/enable")
    @RequirePermission(value = "role.enable", dataScope = DataScopeType.TENANT, auditAction = "ROLE_ENABLE")
    public ApiResponse<String> enable(@PathVariable("id") Long id) {
        systemUserService.enableRole(id);
        return ApiResponse.success("OK");
    }

    @PostMapping("/{id}/disable")
    @RequirePermission(value = "role.disable", dataScope = DataScopeType.TENANT, auditAction = "ROLE_DISABLE")
    public ApiResponse<String> disable(@PathVariable("id") Long id) {
        systemUserService.disableRole(id);
        return ApiResponse.success("OK");
    }

    @PostMapping("/{id}/delete")
    @RequirePermission(value = "role.delete", dataScope = DataScopeType.TENANT, auditAction = "ROLE_DELETE")
    public ApiResponse<String> softDelete(@PathVariable("id") Long id) {
        systemUserService.softDeleteRole(id);
        return ApiResponse.success("OK");
    }

    @GetMapping("/{id}/permissions")
    @RequirePermission(value = "role.read", dataScope = DataScopeType.TENANT, auditAction = "ROLE_PERMISSIONS")
    public ApiResponse<java.util.List<Long>> getRolePermissions(@PathVariable("id") Long id) {
        return ApiResponse.success(systemUserService.getRolePermissionIds(id));
    }

    @PostMapping("/{id}/permissions")
    @RequirePermission(value = "role.permission.bind", dataScope = DataScopeType.TENANT, auditAction = "ROLE_BIND_PERMISSIONS")
    public ApiResponse<String> bindPermissions(@PathVariable("id") Long id, @RequestBody @Valid BindRolePermissionsRequest request) {
        systemUserService.bindRolePermissions(id, request);
        return ApiResponse.success("OK");
    }

    @GetMapping("/{id}/menus")
    @RequirePermission(value = "role.read", dataScope = DataScopeType.TENANT, auditAction = "ROLE_MENUS")
    public ApiResponse<java.util.List<Long>> getRoleMenus(@PathVariable("id") Long id) {
        return ApiResponse.success(menuService.getRoleMenuIds(id));
    }

    @PostMapping("/{id}/menus")
    @RequirePermission(value = "role.permission.bind", dataScope = DataScopeType.TENANT, auditAction = "ROLE_BIND_MENUS")
    public ApiResponse<String> bindMenus(@PathVariable("id") Long id, @RequestBody @Valid BindRoleMenusRequest request) {
        menuService.bindRoleMenus(id, request);
        return ApiResponse.success("OK");
    }
}
