package com.auth.service.base.controller;

import com.auth.common.model.common.result.PageResult;
import com.auth.common.model.common.result.Result;
import com.auth.common.model.common.result.ResultCodeEnum;
import com.auth.dao.base.entity.UserEntity;
import com.auth.model.base.dto.UserDto;
import com.auth.model.base.vo.UserVo;
import com.auth.service.base.service.UserService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 用户信息 前端控制器
 * </p>
 *
 * @author AuthoritySystem
 * @since 2025-07-22 23:14:35
 */
@Tag(name = "用户信息", description = "用户信息相关接口")
@RestController
@RequestMapping("/api/v1/base/user")
@RequiredArgsConstructor
public class UserController {

    private final UserService userService;

    @PreAuthorize("auth.decide('user:query')")
    @Operation(summary = "分页查询用户信息", description = "分页查询用户信息")
    @GetMapping()
    public Result<PageResult<UserVo>> getUserPage(UserDto dto) {
        Page<UserEntity> pageParams = new Page<>(dto.getPageIndex(), dto.getPageSize());
        PageResult<UserVo> pageResult = userService.getUserPage(pageParams, dto);
        return Result.success(pageResult);
    }

    @PreAuthorize("auth.decide('user:query')")
    @Operation(summary = "根据id查询用户信息详情", description = "根据id查询用户信息详情")
    @GetMapping("{id}")
    public Result<UserVo> getById(@PathVariable("id") Long id) {
        UserVo userVo = userService.getById(id);

        return Result.success(userVo);
    }

    @PreAuthorize("auth.decide('user:create')")
    @Operation(summary = "添加用户信息", description = "添加用户信息")
    @PostMapping()
    public Result<String> create(@Valid @RequestBody UserDto dto) {
        userService.create(dto);
        return Result.success(ResultCodeEnum.ADD_SUCCESS);
    }

    @PreAuthorize("auth.decide('user:update')")
    @Operation(summary = "更新用户信息", description = "更新用户信息")
    @PutMapping()
    public Result<String> update(@Valid @RequestBody UserDto dto) {
        userService.update(dto);
        return Result.success(ResultCodeEnum.UPDATE_SUCCESS);
    }

    @PreAuthorize("auth.decide('user:delete')")
    @Operation(summary = "删除用户信息", description = "删除用户信息")
    @DeleteMapping()
    public Result<String> batchDelete(@RequestBody List<Long> ids) {
        userService.batchDelete(ids);
        return Result.success(ResultCodeEnum.DELETE_SUCCESS);
    }
}