package top.tqx.demo_1.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;

import java.util.List;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import top.tqx.demo_1.common.PageResult;
import top.tqx.demo_1.common.Result;
import top.tqx.demo_1.dto.UserCreateRequest;
import top.tqx.demo_1.dto.UserUpdateRequest;
import top.tqx.demo_1.service.UserService;
import top.tqx.demo_1.vo.UserVO;

@Tag(name = "用户管理", description = "用户的增删改查操作")
@RestController
@RequestMapping("/user")
public class UserController extends BaseController {

    @Autowired
    private UserService userService;

    @Operation(summary = "分页查询用户", description = "根据条件分页查询用户列表")
    @GetMapping("/page")
    public Result<PageResult<UserVO>> pageUsers(
            @Parameter(description = "页码", example = "1") @RequestParam(defaultValue = "1") Integer pageNum,
            @Parameter(description = "每页大小", example = "10") @RequestParam(defaultValue = "10") Integer pageSize,
            @Parameter(description = "用户名") @RequestParam(required = false) String username,
            @Parameter(description = "部门ID") @RequestParam(required = false) Long deptId) {
        PageResult<UserVO> result = userService.pageUsers(pageNum, pageSize, username, deptId);
        return success(result);
    }

    @Operation(summary = "查询用户详情", description = "根据ID查询用户详细信息")
    @GetMapping("/{id}")
    public Result<UserVO> getUserById(@Parameter(description = "用户ID") @PathVariable Long id) {
        UserVO user = userService.getUserById(id);
        return success(user);
    }

    @Operation(summary = "创建用户", description = "创建新用户，默认密码为123456")
    @PostMapping
    public Result<Void> createUser(@Valid @RequestBody UserCreateRequest request) {
        userService.createUser(request);
        return success();
    }

    @Operation(summary = "更新用户", description = "更新用户基本信息")
    @PutMapping
    public Result<Void> updateUser(@Valid @RequestBody UserUpdateRequest request) {
        userService.updateUser(request);
        return success();
    }

    @Operation(summary = "删除用户", description = "根据ID删除用户")
    @DeleteMapping("/{id}")
    public Result<Void> deleteUser(@Parameter(description = "用户ID") @PathVariable Long id) {
        userService.deleteUser(id);
        return success();
    }

    @Operation(summary = "重置密码", description = "重置用户密码为默认密码123456")
    @PutMapping("/{id}/reset-password")
    public Result<Void> resetPassword(@Parameter(description = "用户ID") @PathVariable Long id) {
        userService.resetPassword(id);
        return success();
    }

    @Operation(summary = "更新用户状态", description = "启用或禁用用户")
    @PutMapping("/{id}/status")
    public Result<Void> updateStatus(
            @Parameter(description = "用户ID") @PathVariable Long id,
            @Parameter(description = "状态：1启用 0禁用") @RequestParam Integer status) {
        userService.updateStatus(id, status);
        return success();
    }

    @Operation(summary = "分配用户角色", description = "为用户分配角色")
    @PutMapping("/{id}/roles")
    public Result<Void> assignRoles(
            @Parameter(description = "用户ID") @PathVariable Long id,
            @Parameter(description = "角色ID列表") @RequestBody List<Long> roleIds) {
        userService.assignRoles(id, roleIds);
        return success();
    }
}
