package com.njcn.rdms.module.system.controller.admin.user;

import com.njcn.rdms.framework.common.pojo.CommonResult;
import com.njcn.rdms.module.system.controller.admin.user.vo.preference.UserPreferenceThemeRespVO;
import com.njcn.rdms.module.system.controller.admin.user.vo.preference.UserPreferenceThemeSaveReqVO;
import com.njcn.rdms.module.system.service.user.UserPreferenceService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
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 static com.njcn.rdms.framework.common.pojo.CommonResult.success;
import static com.njcn.rdms.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId;

@Tag(name = "管理后台 - 用户主题偏好")
@RestController
@RequestMapping("/system/user-preference")
@Validated
public class UserPreferenceController {

    @Resource
    private UserPreferenceService userPreferenceService;

    @GetMapping("/theme")
    @Operation(summary = "获得当前登录用户主题配置")
    public CommonResult<UserPreferenceThemeRespVO> getTheme() {
        // 只允许读取当前登录用户自己的主题配置，不提供按 userId 查询他人配置的入口。
        return success(UserPreferenceThemeRespVO.of(userPreferenceService.getThemeSettings(getLoginUserId())));
    }

    @PutMapping("/theme")
    @Operation(summary = "保存当前登录用户主题配置")
    public CommonResult<Boolean> saveTheme(@Valid @RequestBody UserPreferenceThemeSaveReqVO reqVO) {
        // 保存归属始终以后端登录态为准，禁止前端通过请求体指定目标用户。
        userPreferenceService.saveThemeSettings(getLoginUserId(), reqVO.getThemeSettings());
        return success(true);
    }

    @DeleteMapping("/theme")
    @Operation(summary = "重置当前登录用户主题配置")
    public CommonResult<Boolean> resetTheme() {
        // 重置语义是清空当前用户的个性化覆盖项，前端随后回退到默认主题配置。
        userPreferenceService.resetThemeSettings(getLoginUserId());
        return success(true);
    }

}
