package shop.alien.storeplatform.controller;

import io.swagger.annotations.*;
import lombok.Data;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import shop.alien.entity.result.R;
import shop.alien.storeplatform.service.StorePlatformRoleMenuService;

import java.util.List;

/**
 * 角色菜单关联表 前端控制器
 *
 * @author system
 * @since 2025-01-XX
 */
@Slf4j
@Api(tags = {"平台-角色菜单关联管理"})
@ApiSort(1)
@CrossOrigin
@RestController
@RequestMapping("/platform/role-menu")
@RequiredArgsConstructor
public class StorePlatformRoleMenuController {

    private final StorePlatformRoleMenuService storePlatformRoleMenuService;

    @ApiOperation("根据角色ID查询菜单ID列表")
    @ApiOperationSupport(order = 1)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "roleId", value = "角色ID", dataType = "Long", paramType = "query", required = true)
    })
    @GetMapping("/getMenuIdsByRoleId")
    public R<List<Long>> getMenuIdsByRoleId(@RequestParam("roleId") Long roleId) {
        log.info("StorePlatformRoleMenuController.getMenuIdsByRoleId?roleId={}", roleId);
        List<Long> menuIds = storePlatformRoleMenuService.getMenuIdsByRoleId(roleId);
        return R.data(menuIds);
    }

    @ApiOperation("根据菜单ID查询角色ID列表")
    @ApiOperationSupport(order = 2)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "menuId", value = "菜单ID", dataType = "Long", paramType = "query", required = true)
    })
    @GetMapping("/getRoleIdsByMenuId")
    public R<List<Long>> getRoleIdsByMenuId(@RequestParam("menuId") Long menuId) {
        log.info("StorePlatformRoleMenuController.getRoleIdsByMenuId?menuId={}", menuId);
        List<Long> roleIds = storePlatformRoleMenuService.getRoleIdsByMenuId(menuId);
        return R.data(roleIds);
    }

    @ApiOperation("为角色分配菜单")
    @ApiOperationSupport(order = 3)
    @PostMapping("/assignMenus")
    public R<String> assignMenus(@RequestBody AssignMenusDto assignMenusDto) {
        log.info("StorePlatformRoleMenuController.assignMenus?roleId={}, menuIds={}", 
                assignMenusDto.getRoleId(), assignMenusDto.getMenuIds());
        boolean result = storePlatformRoleMenuService.assignMenus(assignMenusDto.getRoleId(), assignMenusDto.getMenuIds());
        if (result) {
            return R.success("分配成功");
        }
        return R.fail("分配失败");
    }

    @ApiOperation("移除角色的所有菜单")
    @ApiOperationSupport(order = 4)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "roleId", value = "角色ID", dataType = "Long", paramType = "query", required = true)
    })
    @DeleteMapping("/removeAllMenus")
    public R<String> removeAllMenus(@RequestParam("roleId") Long roleId) {
        log.info("StorePlatformRoleMenuController.removeAllMenus?roleId={}", roleId);
        boolean result = storePlatformRoleMenuService.removeAllMenus(roleId);
        if (result) {
            return R.success("移除成功");
        }
        return R.fail("移除失败");
    }

    @ApiOperation("为角色添加单个菜单")
    @ApiOperationSupport(order = 5)
    @PostMapping("/addMenu")
    public R<String> addMenu(@RequestBody AddMenuDto addMenuDto) {
        log.info("StorePlatformRoleMenuController.addMenu?roleId={}, menuId={}", 
                addMenuDto.getRoleId(), addMenuDto.getMenuId());
        boolean result = storePlatformRoleMenuService.addMenu(addMenuDto.getRoleId(), addMenuDto.getMenuId());
        if (result) {
            return R.success("添加成功");
        }
        return R.fail("添加失败");
    }

    @ApiOperation("移除角色的单个菜单")
    @ApiOperationSupport(order = 6)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "roleId", value = "角色ID", dataType = "Long", paramType = "query", required = true),
            @ApiImplicitParam(name = "menuId", value = "菜单ID", dataType = "Long", paramType = "query", required = true)
    })
    @DeleteMapping("/removeMenu")
    public R<String> removeMenu(@RequestParam("roleId") Long roleId, @RequestParam("menuId") Long menuId) {
        log.info("StorePlatformRoleMenuController.removeMenu?roleId={}, menuId={}", roleId, menuId);
        boolean result = storePlatformRoleMenuService.removeMenu(roleId, menuId);
        if (result) {
            return R.success("移除成功");
        }
        return R.fail("移除失败");
    }

    /**
     * 分配菜单请求DTO
     */
    @Data
    @ApiModel(value = "AssignMenusDto", description = "分配菜单请求参数")
    static class AssignMenusDto {
        @ApiModelProperty(value = "角色ID", required = true)
        private Long roleId;

        @ApiModelProperty(value = "菜单ID列表", required = true)
        private List<Long> menuIds;
    }

    /**
     * 添加菜单请求DTO
     */
    @Data
    @ApiModel(value = "AddMenuDto", description = "添加菜单请求参数")
    static class AddMenuDto {
        @ApiModelProperty(value = "角色ID", required = true)
        private Long roleId;

        @ApiModelProperty(value = "菜单ID", required = true)
        private Long menuId;
    }
}

