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.*;
import com.seer.teach.user.admin.response.AdminUserInfoResp;
import com.seer.teach.user.admin.response.AdminUserPageResp;
import com.seer.teach.user.admin.service.AdminUserService;
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.HashMap;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author Captain
 * @since 2025-05-14
 */
@RestController
@RequestMapping("/user")
@Tag(name = "管理端 - 用户管理")
@Validated
@RequiredArgsConstructor
public class AdminUserController {

    private final AdminUserService adminUserService;

    private final AdminAuthorityService adminAuthorityService;

    /**
     * 登录接口
     *
     * @param params
     * @return
     */
    @PostMapping("/login")
    @LogPrint
    @Operation(summary = "登录接口")
    public ResultBean<HashMap<String, Object>> login(@RequestBody @Validated AdminUserLoginReq params) {
        HashMap<String, Object> login = adminUserService.login(params);
        return ResultBean.success(login);
    }

    @PostMapping("/logout")
    @LogPrint
    @SaCheckLogin(type = StpUtil.TYPE)
    @Operation(summary = "登出接口")
    @SaCheckPermission("admin:user:logout")
    public ResultBean logout() {
        adminUserService.logout();
        return ResultBean.success();
    }

    @GetMapping("/info")
    @LogPrint
    @Operation(summary = "获取用户信息")
    @SaCheckPermission("admin:user:info")
    public ResultBean<AdminUserInfoResp> info() {
        AdminUserInfoResp user = adminAuthorityService.info(StpUtil.getLoginIdAsInt(),"seer-admin");
        return ResultBean.success(user);
    }

    @PostMapping("/register")
    @LogPrint
    @SaCheckLogin(type = StpUtil.TYPE)
    @Operation(summary = "添加管理员接口")
    @SaCheckPermission("admin:user:register")
    public ResultBean register(@RequestBody @Validated RegisterAdminReq adminUserReq) {
        adminUserService.register(adminUserReq);
        return ResultBean.success();
    }

    @PostMapping("/update/{id}")
    @LogPrint
    @SaCheckLogin(type = StpUtil.TYPE)
    @Operation(summary = "修改管理员信息")
    @SaCheckPermission("admin:user:update")
    public ResultBean update(@RequestBody @Validated AdminUpdateReq params,
                             @PathVariable("id") Integer id) {
        adminUserService.updateAdminById(params, id);
        return ResultBean.success();
    }

    @PutMapping("/{id}/password/reset")
    @LogPrint
    @SaCheckLogin(type = StpUtil.TYPE)
    @Operation(summary = "重置密码")
    @SaCheckPermission("admin:user:password:reset")
    public ResultBean resetPassword(@RequestBody @Validated AdminUpdateReq params,
                             @PathVariable("id") Integer id) {
        adminUserService.resetPassword(params, id);
        return ResultBean.success();
    }

    @PostMapping("/delete/{id}")
    @LogPrint
    @SaCheckLogin(type = StpUtil.TYPE)
    @Operation(summary = "删除管理员信息")
    @SaCheckPermission("admin:user:delete")
    public ResultBean<Boolean> delete(@PathVariable("id") Integer id) {
        boolean result = adminUserService.removeById(id);
        if (result) {
            return ResultBean.success(result);
        }
        return ResultBean.error();
    }

    @PostMapping("/updateStatus/{id}")
    @LogPrint
    @SaCheckLogin(type = StpUtil.TYPE)
    @Operation(summary = "修改管理员状态")
    @SaCheckPermission("admin:user:update:status")
    public ResultBean updateStatus(@PathVariable("id") Integer id,
                                   @RequestParam("status") Integer status) {
        adminUserService.updateStatus(id, status);
        return ResultBean.success();
    }

    @GetMapping("/list/{page}/{size}")
    @LogPrint
    @SaCheckLogin(type = StpUtil.TYPE)
    @Operation(summary = "获取管理员分页列表")
    @SaCheckPermission("admin:user:paged")
    public ResultBean<PageListBean<AdminUserPageResp>> paged(
            @Parameter(description = "页码") @PathVariable("page") Integer page,
            @Parameter(description = "每页数量") @PathVariable("size") Integer size,
            @Parameter(description = "账号") @RequestParam(value = "keyword", required = false) String keyword) {
        return ResultBean.success(adminUserService.listPage(page, size, keyword));
    }

    @PostMapping("/list")
    @LogPrint
    @SaCheckLogin(type = StpUtil.TYPE)
    @Operation(summary = "人员列表查询")
    @SaCheckPermission("admin:user:list")
    public ResultBean<PageListBean<AdminUserPageResp>> list(@RequestBody AdminUserSearchReq searchReq){
        return ResultBean.success(adminUserService.list(searchReq));
    }

    @PostMapping("/getUserListByRoleId")
    @LogPrint
    @SaCheckLogin(type = StpUtil.TYPE)
    @Operation(summary = "人员列表查询")
    @SaCheckPermission("admin:user:role-id")
    public ResultBean<PageListBean<AdminUserPageResp>> getUserListByRoleId(@RequestBody AdminUserSearchReq searchReq){
        return ResultBean.success(adminUserService.getUserListByRoleId(searchReq));
    }

    @GetMapping("/listByRole/{pageSize}/{pageNo}")
    @LogPrint
    @SaCheckLogin(type = StpUtil.TYPE)
    @Operation(summary = "根据角色编码查询人员列表")
    @SaCheckPermission("admin:user:listByRole")
    public ResultBean<PageListBean<AdminUserPageResp>> listByRole(
            @Parameter(description = "页码") @PathVariable("pageNo") Integer pageNo,
            @Parameter(description = "每页数量") @PathVariable("pageSize") Integer pageSize,
            @Parameter(description = "角色编码") @RequestParam(value = "roleCode") String roleCode) {
        return ResultBean.success(adminUserService.listByRole(pageNo, pageSize, roleCode));
    }
}