package com.yaoyuan.jiscuss.controller.api;

import com.yaoyuan.jiscuss.dto.UserCreateRequest;
import com.yaoyuan.jiscuss.dto.UserMapper;
import com.yaoyuan.jiscuss.dto.UserResponse;
import com.yaoyuan.jiscuss.entity.User;
import com.yaoyuan.jiscuss.exception.BaseException;
import com.yaoyuan.jiscuss.response.ApiResponse;
import com.yaoyuan.jiscuss.response.ResponseCode;
import com.yaoyuan.jiscuss.service.IUsersService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/user_api")
@Tag(name = "用户接口管理", description = "用户 CRUD API")
public class RestUserController {

    private static final Logger logger = LoggerFactory.getLogger(RestUserController.class);

    @Autowired
    private IUsersService usersService;

    @Autowired
    private UserMapper userMapper;

    @PostMapping("/user")
    @Operation(summary = "新增用户")
    public ApiResponse<UserResponse> save(@Valid @RequestBody UserCreateRequest request) {
        User user = userMapper.fromCreateRequest(request);
        User saved = usersService.insert(user);
        if (saved != null) {
            return ApiResponse.ok(userMapper.toResponse(saved));
        }
        throw new BaseException(ResponseCode.RESOURCES_NOT_EXIST);
    }

    @DeleteMapping("/user/{id}")
    @Operation(summary = "删除用户")
    public ApiResponse<Boolean> delete(@PathVariable Integer id) {
        usersService.remove(id);
        return ApiResponse.ok(true);
    }

    @PutMapping("/user/{id}")
    @Operation(summary = "修改用户")
    public ApiResponse<UserResponse> update(@RequestBody User user, @PathVariable Integer id) {
        User updated = usersService.update(user, id);
        if (updated != null) {
            return ApiResponse.ok(userMapper.toResponse(updated));
        }
        throw new BaseException(ResponseCode.RESOURCES_NOT_EXIST);
    }

    @GetMapping("/user/{id}")
    @Operation(summary = "获取用户")
    public ApiResponse<UserResponse> getUser(@PathVariable Integer id) {
        User user = usersService.findOne(id);
        logger.info("获取用户==>：{}", user);
        return ApiResponse.ok(userMapper.toResponse(user));
    }

    @GetMapping("/user")
    @Operation(summary = "获取全部用户")
    public ApiResponse<List<UserResponse>> getAllUsers() {
        List<UserResponse> list = usersService.getAllList().stream()
                .map(userMapper::toResponse)
                .toList();
        return ApiResponse.ok(list);
    }
}
