package org.xyzh.system.controller;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
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 org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.xyzh.api.system.permission.PermissionService;
import org.xyzh.common.core.domain.ResultDomain;
import org.xyzh.common.dto.menu.TbSysMenu;
import org.xyzh.common.dto.menu.TbSysMenuPermission;
import org.xyzh.common.dto.permission.TbSysPermission;
import org.xyzh.common.dto.role.TbSysRole;
import org.xyzh.common.dto.role.TbSysRolePermission;
import org.xyzh.common.vo.PermissionVO;

@RestController
@RequestMapping("/permissions")
public class PermissionController {
    private Logger logger = LoggerFactory.getLogger(PermissionController.class);

    @Autowired
    private PermissionService permissionService;

    /**
     * @description    创建权限
     * @param permission
     * @author    yslg
     * @ since 2025-10-06
     */
    @PostMapping("/permission")
    public ResultDomain<TbSysPermission> createPermission(@RequestBody TbSysPermission permission) {
        return permissionService.createPermission(permission);
    }

        /**
     * @description    获取权限
     * @param permission
     * @author    yslg
     * @ since 2025-10-06
     */
    @GetMapping("/permission")
    public ResultDomain<PermissionVO> getPermission(@RequestParam PermissionVO permission) {
        return permissionService.getPermissionVO(permission);
    }

    /**
     * @description    更新权限
     * @param permission
     * @author    yslg
     * @ since 2025-10-06
     */
    @PutMapping("/permission")
    public ResultDomain<TbSysPermission> updatePermission(@RequestBody TbSysPermission permission) {
        return permissionService.updatePermission(permission);
    }

    /**
     * @description    删除权限
     * @param permissionID
     * @author    yslg
     * @ since 2025-10-06
     */
    @DeleteMapping("/permission")
    public ResultDomain<TbSysPermission> deletePermission(@RequestBody String permissionID) {
        return permissionService.deletePermission(permissionID);
    }

    /**
     * @description    查询权限列表
     * @param permission
     * @author    yslg
     * @ since 2025-10-06
     */
    @PostMapping("/list")
    public ResultDomain<PermissionVO> listPermission(@RequestBody PermissionVO permission) {
        return permissionService.listPermission(permission);
    }

    /**
     * @description    查询权限绑定列表
     * @param permission
     * @author    yslg
     * @ since 2025-10-06
     */
    @PostMapping("/list/binding")
    public ResultDomain<PermissionVO> listPermissionBinding(@RequestBody PermissionVO permission) {
        return permissionService.listPermissionBinding(permission);
    }

    /**
     * @description    绑定菜单
     * @param permission
     * @author    yslg
     * @ since 2025-10-06
     */
    @PostMapping("/bind/menu")
    public ResultDomain<TbSysMenuPermission> bindMenu(@RequestBody PermissionVO permission) {
        List<String> menuIds = permission.getMenus().stream().map(TbSysMenu::getMenuID).collect(Collectors.toList());
        List<String> permissionIds = permission.getPermissions().stream().map(TbSysPermission::getPermissionID).collect(Collectors.toList());
        return permissionService.batchBindMenuPermission(menuIds, permissionIds);
    }

    /**
     * @description    解绑菜单
     * @param permission
     * @author    yslg
     * @ since 2025-10-06
     */
    @PostMapping("/unbind/menu")
    public ResultDomain<TbSysMenuPermission> unbindMenu(@RequestBody PermissionVO permission) {
        List<String> menuIds = permission.getMenus().stream().map(TbSysMenu::getMenuID).collect(Collectors.toList());
        List<String> permissionIds = permission.getPermissions().stream().map(TbSysPermission::getPermissionID).collect(Collectors.toList());
        return permissionService.batchUnbindMenuPermission(menuIds, permissionIds);
    }

    /**
     * @description    绑定角色
     * @param permission
     * @author    yslg
     * @ since 2025-10-06
     */
    @PostMapping("/bind/role")
    public ResultDomain<TbSysRolePermission> bindRole(@RequestBody PermissionVO permission) {
        List<String> roleIds = permission.getRoles().stream().map(TbSysRole::getRoleID).collect(Collectors.toList());
        List<String> permissionIds = permission.getPermissions().stream().map(TbSysPermission::getPermissionID).collect(Collectors.toList());
        return permissionService.batchBindRolePermission(roleIds, permissionIds);
    }

    /**
     * @description    解绑角色
     * @param permission
     * @author    yslg
     * @ since 2025-10-06
     */
    @PostMapping("/unbind/role")
    public ResultDomain<TbSysRolePermission> unbindRole(@RequestBody PermissionVO permission) {
        List<String> roleIds = permission.getRoles().stream().map(TbSysRole::getRoleID).collect(Collectors.toList());
        List<String> permissionIds = permission.getPermissions().stream().map(TbSysPermission::getPermissionID).collect(Collectors.toList());
        return permissionService.batchUnbindRolePermission(roleIds, permissionIds);
    }
    
}
