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.AddAuthority;
import com.seer.teach.user.admin.request.AuthorityBatchUpdate;
import com.seer.teach.user.admin.request.UpdateAuthority;
import com.seer.teach.user.admin.response.AuthorityRespData;
import com.seer.teach.user.admin.service.AdminAuthorityService;
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-07-01 20:38
 */
@RestController
@RequestMapping("/user/authority")
@Tag(name = "管理端 - 权限管理")
@SaCheckLogin(type = StpUtil.TYPE)
@Validated
@RequiredArgsConstructor
@LogPrint
public class AdminAuthorityController {

    private final AdminAuthorityService adminAuthorityService;

    @GetMapping("/list/{page}/{size}")
    @Operation(summary = "分页查询获取权限列表")
    @SaCheckPermission("admin:authority:list")
    public ResultBean<PageListBean<AuthorityRespData>> getAuthority(
            @Parameter(description = "页码") @PathVariable("page") Integer page,
            @Parameter(description = "每页数量") @PathVariable("size") Integer size,
            @Parameter(description = "权限名称") @RequestParam(required = false) String authorityName) {
        return ResultBean.success(adminAuthorityService.getAuthorityPage(page,size,authorityName));
    }

    @PostMapping("/createAuthority")
    @Operation(summary = "添加权限")
    @SaCheckPermission("admin:authority:create")
    public ResultBean createAuthority(@RequestBody @Validated AddAuthority params){
        adminAuthorityService.addSystemAuthority(params);
        return ResultBean.success();
    }

    @DeleteMapping("/delete/{id}")
    @Operation(summary = "根据id删除权限")
    @SaCheckPermission("admin:authority:delete")
    public ResultBean deleteAuthorityById(@PathVariable Integer id){
        adminAuthorityService.deleteAuthorityById(id);
        return ResultBean.success();
    }

    @PostMapping("/update")
    @Operation(summary = "修改权限")
    @SaCheckPermission("admin:authority:update")
    public ResultBean updateAuthority(@RequestBody @Validated UpdateAuthority params){
        adminAuthorityService.updateAuthority(params);
        return ResultBean.success();
    }

    @PutMapping("/{id}/toggle-show")
    @Operation(summary = "切换权限显示状态")
    @SaCheckPermission("admin:authority:toggle-show")
    public ResultBean<Boolean> toggleShowAuthority(@PathVariable Integer id,@RequestParam("isShow") Integer isShow) {
        return ResultBean.success(adminAuthorityService.toggleShowAuthority(id,isShow));
    }

    @PutMapping("/batch-update")
    @Operation(summary = "修改权限")
    @SaCheckPermission("admin:authority:batch-update")
    public ResultBean<Boolean> batchUpdateAuthority(@RequestBody @Validated AuthorityBatchUpdate batchUpdateParams){
        return ResultBean.success(adminAuthorityService.batchUpdateAuthority(batchUpdateParams));
    }

    @GetMapping("/menu/tree-list")
    @Operation(summary = "获取全部权限树结构")
    @SaCheckPermission("admin:authority:tree:list")
    public ResultBean<List<AuthorityRespData>> getAllAuthorityTree(){
        List<AuthorityRespData> tree = adminAuthorityService.getSystemAllAuthority();
        return ResultBean.success(tree);
    }
}