package com.emotion.controller;

import com.emotion.common.PageResult;
import com.emotion.common.Result;
import com.emotion.dto.request.UserCreateRequest;
import com.emotion.dto.request.UserPageRequest;
import com.emotion.dto.request.UserProfileUpdateRequest;
import com.emotion.dto.request.UserUpdateRequest;
import com.emotion.dto.response.UserResponse;
import com.emotion.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * 用户控制器
 *
 * @author huazhongmin
 * @date 2025-07-22
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    /**
     * 分页查询用户
     */
    @GetMapping(value = "/page")
    public Result<PageResult<UserResponse>> getPage(@Validated UserPageRequest request) {
        PageResult<UserResponse> pageResult = userService.getPageWithResponse(request);
        return Result.success(pageResult);
    }

    /**
     * 根据ID获取用户信息
     */
    @GetMapping(value = "/detail")
    public Result<UserResponse> getById(@RequestParam String id) {
        UserResponse user = userService.getUserResponseById(id);
        if (user == null) {
            return Result.notFound("用户不存在");
        }
        return Result.success(user);
    }

    /**
     * 创建用户
     */
    @PostMapping(value = "/create")
    public Result<UserResponse> create(@Valid @RequestBody UserCreateRequest request) {
        UserResponse user = userService.createUserWithResponse(request);
        return Result.success(user);
    }

    /**
     * 更新用户
     */
    @PutMapping(value = "/update")
    public Result<UserResponse> update(@Valid @RequestBody UserUpdateRequest request) {
        UserResponse updatedUser = userService.updateUserWithResponse(request);
        if (updatedUser == null) {
            return Result.error("更新失败");
        }
        return Result.success(updatedUser);
    }

    /**
     * 删除用户
     */
    @DeleteMapping(value = "/delete")
    public Result<Void> delete(@RequestParam String id) {
        boolean deleted = userService.deleteUser(id);
        if (!deleted) {
            return Result.error("删除失败");
        }
        return Result.success();
    }

    /**
     * 获取当前用户个人资料
     */
    @GetMapping(value = "/profile")
    public Result<UserResponse> getCurrentUserProfile() {
        UserResponse user = userService.getCurrentUserProfileWithResponse();
        if (user == null) {
            return Result.unauthorized("用户未登录");
        }
        return Result.success(user);
    }

    /**
     * 更新当前用户个人资料
     */
    @PutMapping(value = "/profile")
    public Result<UserResponse> updateCurrentUserProfile(@Valid @RequestBody UserProfileUpdateRequest request) {
        UserResponse updatedUser = userService.updateCurrentUserProfileWithResponse(request);
        if (updatedUser == null) {
            return Result.unauthorized("用户未登录");
        }
        return Result.success(updatedUser);
    }
}