package com.red.circle.console.adapter.admin;

import com.red.circle.console.app.dto.clienobject.admin.ConsoleTokenCO;
import com.red.circle.console.app.dto.clienobject.admin.MenuTreeCO;
import com.red.circle.console.app.dto.clienobject.admin.UserDetailsCO;
import com.red.circle.console.app.dto.cmd.admin.AccountInfoCmd;
import com.red.circle.console.app.dto.cmd.admin.PasswordCmd;
import com.red.circle.console.app.service.admin.UserAccountService;
import com.red.circle.console.infra.annotations.OpsOperationReqLog;
import com.red.circle.console.inner.model.cmd.LoginCmd;
import com.red.circle.framework.core.dto.CommonCommand;
import com.red.circle.framework.web.controller.BaseController;
import java.util.List;
import java.util.Set;
import jakarta.servlet.http.HttpServletRequest;
import lombok.RequiredArgsConstructor;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
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;

/**
 * 账户前端控制器.
 *
 * @author pengliang
 */
@Validated
@RestController
@RequestMapping(value = "/account", produces = MediaType.APPLICATION_JSON_VALUE)
@RequiredArgsConstructor
public class AccountRestController extends BaseController {

  private final UserAccountService userAccountService;

  /**
   * 登录.
   */
  @PostMapping("/login")
  public ConsoleTokenCO login(@RequestBody @Validated LoginCmd cmd) {
      return userAccountService.login(cmd);
  }

  /**
   * 退出登录.
   */
  @PostMapping("/logout")
  public void logout() {
  }

  /**
   * 修改密码.
   */
  @PutMapping("/password")
//  @OpsOperationReqLog(value = "修改密码", ignoreRequestParam = true)
  @OpsOperationReqLog(value = "修改密码", ignoreRequestParam = false)
  public void updatePassword(@RequestBody @Validated PasswordCmd cmd) {
    userAccountService.updatePassword(cmd);
  }

  /**
   * 登录用户信息.
   */
  @GetMapping("/info")
  public UserDetailsCO accountInfo(CommonCommand cmd) {
    return userAccountService.getUserDetails(cmd);
  }

  /**
   * 修改账户信息.
   */
  @PutMapping("/info")
  @OpsOperationReqLog(value = "修改账户信息", ignoreRequestParam = true)
  public void accountInfo(@RequestBody @Validated AccountInfoCmd cmd) {
    userAccountService.updateAccountInfo(cmd);
  }

  /**
   * 获取账户菜单.
   */
  @GetMapping("/menus")
  public List<MenuTreeCO> menus(CommonCommand cmd) {
    return userAccountService.listAccountMenus(cmd);
  }

  /**
   * 获取账户按钮.
   */
  @GetMapping("/buttons/aliases")
  public Set<String> buttonsAliases(CommonCommand cmd) {
    return userAccountService.listAccountButtonsAliases(cmd);
  }

}

