package cn.reghao.bnt.web.admin.controller;

import cn.reghao.bnt.web.admin.model.dto.UpdatePasswordDto;
import cn.reghao.bnt.web.admin.model.dto.UpdateUsernameDto;
import cn.reghao.bnt.web.admin.model.po.UserMessage;
import cn.reghao.bnt.web.admin.model.vo.LoginRecordInfo;
import cn.reghao.bnt.web.admin.service.*;
import cn.reghao.jutil.jdk.web.db.PageList;
import cn.reghao.jutil.jdk.web.result.Result;
import cn.reghao.jutil.jdk.web.result.ResultStatus;
import cn.reghao.jutil.web.WebResult;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.data.domain.Page;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author reghao
 * @date 2024-08-30 17:52:34
 */
@Tag(name = "用户帐号页面")
@RestController
@RequestMapping("/api/devops/account")
public class AccountController {
    private final AccountService accountService;
    private final AccountSessionService accountSessionService;
    private final UserMessageService userMessageService;
    private final LoginAttemptService loginAttemptService;
    private final int pageSize = 10;

    public AccountController(AccountService accountService, AccountSessionService accountSessionService,
                             UserMessageService userMessageService, LoginAttemptService loginAttemptService) {
        this.accountService = accountService;
        this.accountSessionService = accountSessionService;
        this.userMessageService = userMessageService;
        this.loginAttemptService = loginAttemptService;
    }

    @Operation(summary = "修改帐号", description = "N")
    @PostMapping(value = "/update/username", produces = MediaType.APPLICATION_JSON_VALUE)
    public String updateUsername(@Validated UpdateUsernameDto updateUsernameDto) {
        Result result = accountService.updateMyUsername(updateUsernameDto);
        if (result.getCode() == ResultStatus.SUCCESS.getCode()) {
            // 登出当前会话
            accountSessionService.deactivateUserSession(UserContext.getUserId());
        }
        return WebResult.result(result);
    }

    @Operation(summary = "修改密码", description = "N")
    @PostMapping(value = "/update/passwd", produces = MediaType.APPLICATION_JSON_VALUE)
    public String updatePasswd(@Validated UpdatePasswordDto updatePasswordDto) {
        Result result = accountService.updateMyPassword(updatePasswordDto);
        if (result.getCode() == ResultStatus.SUCCESS.getCode()) {
            // 登出当前会话
            accountSessionService.deactivateUserSession(UserContext.getUserId());
        }
        return WebResult.result(result);
    }

    @Operation(summary = "帐号登入记录列表页面", description = "N")
    @GetMapping("/record")
    public String loginRecordPage() {
        List<LoginRecordInfo> list = loginAttemptService.getActiveLogin();
        return WebResult.success(list);
    }

    @Operation(summary = "登出会话", description = "N")
    @PostMapping(value = "/logout", produces = MediaType.APPLICATION_JSON_VALUE)
    public String logout(String loginId) {
        accountSessionService.deactivateUserLogin(loginId);
        return WebResult.success();
    }

    @Operation(summary = "用户消息列表页面", description = "N")
    @GetMapping("/message")
    public String messagePage(@RequestParam("pn") int pageNumber) {
        Page<UserMessage> page = userMessageService.getMessages(pageNumber, pageSize);
        PageList<UserMessage> pageList = getPageList(page);
        return WebResult.success(pageList);
    }

    private PageList<UserMessage> getPageList(Page<UserMessage> page) {
        int pageNumber = page.getNumber() + 1;
        int pageSize = page.getSize();
        long total = page.getTotalElements();
        List<UserMessage> list = page.getContent();
        return PageList.pageList(pageNumber, pageSize, (int) total, list);
    }

    @Operation(summary = "删除用户所有消息", description = "N")
    @PostMapping(value = "/message/clear", produces = MediaType.APPLICATION_JSON_VALUE)
    public String clearUserMessage() {
        userMessageService.clearUserMessage();
        return WebResult.success();
    }
}
