package com.vetti.web.controller.hotake;

import com.vetti.common.core.domain.AjaxResult;
import com.vetti.common.core.domain.R;
import com.vetti.common.utils.MessageUtils;
import com.vetti.hotake.domain.HotakeSecuritySettings;
import com.vetti.hotake.domain.dto.SecurityChangePasswordDto;
import com.vetti.hotake.domain.vo.SecuritySessionVo;
import com.vetti.hotake.service.IHotakeSecurityService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 安全设置控制器
 *
 * @author vetti
 * @date 2026-02-02
 */
@Api(tags = "安全设置模块")
@RestController
@RequestMapping("/security")
public class HotakeSecurityController
{
    @Autowired
    private IHotakeSecurityService securityService;

    /**
     * 获取当前用户的安全设置
     */
    @ApiOperation("获取当前用户的安全设置")
    @GetMapping("/settings")
    public R<HotakeSecuritySettings> getSecuritySettings()
    {
        HotakeSecuritySettings settings = securityService.getCurrentUserSecuritySettings();
        // 脱敏处理，不返回敏感信息
        settings.setTwoFactorSecret(null);
        settings.setBackupCodes(null);
        settings.setPasswordResetToken(null);
        return R.ok(settings, "");
    }

    /**
     * 更新两步验证设置
     */
    @ApiOperation("更新两步验证设置")
    @PutMapping("/two-factor")
    public AjaxResult updateTwoFactorEnabled(
            @ApiParam(value = "是否启用", required = true)
            @RequestParam Boolean enabled)
    {
        securityService.updateTwoFactorEnabled(enabled);
        return AjaxResult.success(MessageUtils.messageCustomize("HotakeSecurityController10001"));
    }

    /**
     * 修改密码
     */
    @ApiOperation("修改密码")
    @PostMapping("/change-password")
    public AjaxResult changePassword(@Validated @RequestBody SecurityChangePasswordDto dto)
    {
        securityService.changePassword(dto);
        return AjaxResult.success(MessageUtils.messageCustomize("HotakeSecurityController10002"));
    }

    /**
     * 获取活跃会话列表
     */
    @ApiOperation("获取活跃会话列表")
    @GetMapping("/sessions")
    public R<List<SecuritySessionVo>> getActiveSessions()
    {
        List<SecuritySessionVo> sessions = securityService.getActiveSessions();
        return R.ok(sessions, "");
    }

    /**
     * 终止指定会话
     */
    @ApiOperation("终止指定会话")
    @DeleteMapping("/sessions/{sessionId}")
    public AjaxResult terminateSession(
            @ApiParam(value = "会话ID", required = true)
            @PathVariable Long sessionId)
    {
        securityService.terminateSession(sessionId);
        return AjaxResult.success(MessageUtils.messageCustomize("HotakeSecurityController10003"));
    }

    /**
     * 终止所有其他会话
     */
    @ApiOperation("终止所有其他会话")
    @DeleteMapping("/sessions/terminate-all")
    public AjaxResult terminateAllOtherSessions()
    {
        securityService.terminateAllOtherSessions();
        return AjaxResult.success(MessageUtils.messageCustomize("HotakeSecurityController10004"));
    }
}
