package com.writeoff.module.system.controller;

import com.writeoff.common.api.ApiResponse;
import com.writeoff.module.system.dto.ChangePasswordRequest;
import com.writeoff.module.system.dto.UpdateProfilePreferencesRequest;
import com.writeoff.module.system.model.ProfilePreferencesInfo;
import com.writeoff.module.system.service.PlatformIamService;
import com.writeoff.module.system.service.SystemUserService;
import com.writeoff.security.AuthContext;
import com.writeoff.security.AuthScope;
import org.springframework.web.bind.annotation.GetMapping;
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 javax.validation.Valid;

@RestController
@RequestMapping("/api/profile")
public class ProfileController {

    private final SystemUserService systemUserService;
    private final PlatformIamService platformIamService;

    public ProfileController(SystemUserService systemUserService, PlatformIamService platformIamService) {
        this.systemUserService = systemUserService;
        this.platformIamService = platformIamService;
    }

    @GetMapping("/preferences")
    public ApiResponse<ProfilePreferencesInfo> preferences() {
        Long userId = AuthContext.userId();
        if (userId == null) {
            throw new com.writeoff.common.exception.BusinessException(11003, "会话失效");
        }
        if (AuthContext.scope() == AuthScope.PLATFORM) {
            return ApiResponse.success(platformIamService.getMyPreferences(userId));
        }
        return ApiResponse.success(systemUserService.getMyPreferences(userId));
    }

    @PostMapping("/change-password")
    // 这里因为是用户级别操作自身数据，只要拦截器确保已登录，不需要 RequirePermission 进行复杂的权限校验
    // 审计可以由切面或服务层内部记录，这里重点是提供 C 端基础功能
    public ApiResponse<String> changePassword(@RequestBody @Valid ChangePasswordRequest request) {
        Long userId = AuthContext.userId();
        if (userId == null) {
            throw new com.writeoff.common.exception.BusinessException(11003, "会话失效");
        }
        if (AuthContext.scope() == AuthScope.PLATFORM) {
            platformIamService.changeMyPassword(userId, request.getOldPassword(), request.getNewPassword());
            return ApiResponse.success("OK");
        }
        systemUserService.changeMyPassword(userId, request.getOldPassword(), request.getNewPassword());
        return ApiResponse.success("OK");
    }

    @PutMapping("/preferences")
    public ApiResponse<ProfilePreferencesInfo> updatePreferences(@RequestBody UpdateProfilePreferencesRequest request) {
        Long userId = AuthContext.userId();
        if (userId == null) {
            throw new com.writeoff.common.exception.BusinessException(11003, "会话失效");
        }
        if (AuthContext.scope() == AuthScope.PLATFORM) {
            return ApiResponse.success(platformIamService.updateMyPreferences(userId, request));
        }
        return ApiResponse.success(systemUserService.updateMyPreferences(userId, request));
    }
}
