package com.seer.teach.user.admin.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.stp.StpUtil;
import com.seer.teach.common.PageListBean;
import com.seer.teach.common.ResultBean;
import com.seer.teach.common.annotation.LogPrint;
import com.seer.teach.user.admin.request.AddRole;
import com.seer.teach.user.admin.request.UpdateRoleRequest;
import com.seer.teach.user.admin.response.RoleResp;
import com.seer.teach.user.admin.service.AdminRoleService;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author: 嘉多宝宝
 * @Data: 2025-06-30 19:39
 */
@RestController
@RequestMapping("/user/role")
@Tag(name = "管理端 - 角色管理")
@Validated
@SaCheckLogin(type = StpUtil.TYPE)
@RequiredArgsConstructor
public class AdminRoleController {

    private final AdminRoleService adminRoleService;

    @GetMapping("/list/{page}/{size}")
    @LogPrint
    @Operation(summary = "获取管理员角色列表")
    @SaCheckPermission("admin:role:list")
    public ResultBean<PageListBean<RoleResp>> list(
            @Parameter(description = "页码") @PathVariable("page") Integer page,
            @Parameter(description = "每页数量") @PathVariable("size") Integer size,
            @Parameter(description = "角色名称") @RequestParam(required = false) String roleName){
        return ResultBean.success(adminRoleService.listPage(page,size,roleName));
    }

    @PostMapping("/createRole")
    @LogPrint
    @Operation(summary = "添加管理员角色名称")
    @SaCheckPermission("admin:role:create")
    public ResultBean addRole(@RequestBody @Validated AddRole params){
        adminRoleService.addRole(params);
        return ResultBean.success();
    }

    @PostMapping("/update/{id}")
    @LogPrint
    @Operation(summary = "修改角色名称")
    @SaCheckPermission("admin:role:update")
    public ResultBean updateRole(@RequestBody @Validated UpdateRoleRequest params,
                                 @PathVariable Integer id){
        adminRoleService.updateRole(params,id);
        return ResultBean.success();
    }

    @DeleteMapping("/delete/{id}")
    @LogPrint
    @Operation(summary = "删除角色名称")
    @SaCheckPermission("admin:role:delete")
    public ResultBean deleteRoleByRoleId(@PathVariable Integer id){
        adminRoleService.deleteRoleByRoleId(id);
        return ResultBean.success();
    }

    @GetMapping("/listAll")
    @Operation(summary = "查询所有角色名称列表")
    @SaCheckPermission("admin:role:list:all")
    public ResultBean<List<RoleResp>> getAllRoleList() {
        return ResultBean.success(adminRoleService.getAllRoles());
    }

    @GetMapping("/{id}")
    @LogPrint
    @Operation(summary = "根据用户ID查询角色信息")
    @SaCheckPermission("admin:role:info")
    public ResultBean<List<RoleResp>> getRoleByAdminId(@PathVariable Integer id) {
        return ResultBean.success(adminRoleService.getRoleByUserId(id));
    }
}