package com.red.circle.other.adapter.app.activity;

import com.red.circle.common.business.dto.cmd.AppExtCommand;
import com.red.circle.framework.dto.PageResult;
import com.red.circle.framework.web.controller.BaseController;
import com.red.circle.other.app.dto.clientobject.activity.LotteryActivityCO;
import com.red.circle.other.app.dto.clientobject.activity.LotteryActivityDetailCO;
import com.red.circle.other.app.dto.clientobject.activity.LotteryDrawResultCO;
import com.red.circle.other.app.dto.clientobject.activity.LotteryMultiDrawResultCO;
import com.red.circle.other.app.dto.clientobject.activity.LotteryRankCO;
import com.red.circle.other.app.dto.clientobject.activity.LotteryRecordCO;
import com.red.circle.other.app.dto.clientobject.activity.LotteryTicketCO;
import com.red.circle.other.app.dto.clientobject.activity.LotteryWinnerHistoryCO;
import com.red.circle.other.app.dto.clientobject.activity.LotteryWithdrawAmountCO;
import com.red.circle.other.app.dto.clientobject.activity.LotteryWithdrawCO;
import com.red.circle.other.app.dto.cmd.activity.LotteryDrawCmd;
import com.red.circle.other.app.dto.cmd.activity.LotteryMultiDrawCmd;
import com.red.circle.other.app.dto.cmd.activity.LotteryRankQryCmd;
import com.red.circle.other.app.dto.cmd.activity.LotteryRecordQryCmd;
import com.red.circle.other.app.dto.cmd.activity.LotteryWinnerHistoryQryCmd;
import com.red.circle.other.app.dto.cmd.activity.LotteryWithdrawCmd;
import com.red.circle.other.app.dto.cmd.activity.LotteryWithdrawQryCmd;
import com.red.circle.other.app.dto.cmd.activity.LotteryExchangeGoldCmd;
import com.red.circle.other.app.dto.cmd.activity.LotteryTransferCmd;
import com.red.circle.other.app.service.activity.LotteryActivityRestService;

import java.util.List;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 抽奖活动 前端控制器.
 * </p>
 *
 * @author system
 * @eo.api-type http
 * @eo.groupName 活动服务.抽奖活动
 * @eo.path /activity/lottery
 * @since 2025-10-18
 */
@Slf4j
@RestController
@RequestMapping(value = "/activity/lottery", produces = MediaType.APPLICATION_JSON_VALUE)
@RequiredArgsConstructor
public class LotteryActivityRestController extends BaseController {

  private final LotteryActivityRestService lotteryActivityRestService;

  /**
   * 获取有效活动列表.
   *
   * @eo.name 获取有效活动列表.
   * @eo.url /list
   * @eo.method get
   * @eo.request-type formdata
   */
  @GetMapping("/list")
  public List<LotteryActivityCO> listValidActivities(AppExtCommand cmd) {
    return lotteryActivityRestService.listValidActivities(cmd);
  }

  /**
   * 获取有效活动（单个）.
   *
   * @eo.name 获取有效活动（单个）.
   * @eo.url /valid-activity
   * @eo.method get
   * @eo.request-type formdata
   */
  @GetMapping("/valid-activity")
  public LotteryActivityCO getValidActivity(AppExtCommand cmd) {
    List<LotteryActivityCO> activities = lotteryActivityRestService.listValidActivities(cmd);
    return activities.isEmpty() ? null : activities.get(0);
  }

  /**
   * 获取活动详情.
   *
   * @eo.name 获取活动详情.
   * @eo.url /detail/{activityCode}
   * @eo.method get
   * @eo.request-type formdata
   */
  @GetMapping("/detail/{activityCode}")
  public LotteryActivityDetailCO getActivityDetail(@PathVariable String activityCode, AppExtCommand cmd) {
    return lotteryActivityRestService.getActivityDetail(activityCode, cmd);
  }

  /**
   * 执行抽奖.
   *
   * @eo.name 执行抽奖.
   * @eo.url /draw
   * @eo.method post
   * @eo.request-type json
   */
  @PostMapping("/draw")
  public LotteryDrawResultCO draw(@RequestBody @Validated LotteryDrawCmd cmd) {
    return lotteryActivityRestService.draw(cmd);
  }

  /**
   * 执行连抽.
   *
   * @eo.name 执行连抽.
   * @eo.url /multi-draw
   * @eo.method post
   * @eo.request-type json
   */
  @PostMapping("/multi-draw")
  public LotteryMultiDrawResultCO multiDraw(@RequestBody @Validated LotteryMultiDrawCmd cmd) {
    return lotteryActivityRestService.multiDraw(cmd);
  }

  /**
   * 查询我的中奖记录.
   *
   * @eo.name 查询我的中奖记录.
   * @eo.url /my-records
   * @eo.method get
   * @eo.request-type formdata
   */
  @GetMapping("/my-records")
  public PageResult<LotteryRecordCO> listMyRecords(LotteryRecordQryCmd cmd) {
    return lotteryActivityRestService.listMyRecords(cmd);
  }

  /**
   * 查询我的抽奖券列表（详细信息）.
   *
   * @eo.name 查询我的抽奖券列表.
   * @eo.url /my-tickets
   * @eo.method get
   * @eo.request-type formdata
   */
  @GetMapping("/my-tickets")
  public List<LotteryTicketCO> listMyTickets(AppExtCommand cmd) {
    return lotteryActivityRestService.listMyTickets(cmd);
  }

  /**
   * 获取我的剩余抽奖券数量.
   *
   * @eo.name 获取我的剩余抽奖券数量.
   * @eo.url /my-ticket-count
   * @eo.method get
   * @eo.request-type formdata
   */
  @GetMapping("/my-ticket-count")
  public Integer getMyTicketCount(AppExtCommand cmd) {
    return lotteryActivityRestService.getMyTicketCount(cmd);
  }

  /**
   * 查询排行榜.
   *
   * @eo.name 查询排行榜.
   * @eo.url /rank-list
   * @eo.method get
   * @eo.request-type formdata
   */
  @GetMapping("/rank-list")
  public List<LotteryRankCO> getRankList(LotteryRankQryCmd cmd) {
    return lotteryActivityRestService.getRankList(cmd);
  }

  /**
   * 查询历史中奖记录用户（滚动展示）.
   *
   * @eo.name 查询历史中奖记录用户.
   * @eo.url /winner-history
   * @eo.method get
   * @eo.request-type formdata
   */
  @GetMapping("/winner-history")
  public List<LotteryWinnerHistoryCO> getWinnerHistory(LotteryWinnerHistoryQryCmd cmd) {
    return lotteryActivityRestService.getWinnerHistory(cmd);
  }

  /**
   * 提交提现申请.
   *
   * @eo.name 提交提现申请.
   * @eo.url /withdraw/apply
   * @eo.method post
   * @eo.request-type json
   */
  @PostMapping("/withdraw/apply")
  public void applyWithdraw(@RequestBody @Validated LotteryWithdrawCmd cmd) {
    lotteryActivityRestService.applyWithdraw(cmd);
  }

  /**
   * 查询提现记录.
   *
   * @eo.name 查询提现记录.
   * @eo.url /withdraw/records
   * @eo.method get
   * @eo.request-type formdata
   */
  @GetMapping("/withdraw/records")
  public PageResult<LotteryWithdrawCO> listWithdrawRecords(LotteryWithdrawQryCmd cmd) {
    return lotteryActivityRestService.listWithdrawRecords(cmd);
  }

  /**
   * 查询可提现金额.
   *
   * @eo.name 查询可提现金额.
   * @eo.url /withdraw/amount
   * @eo.method get
   * @eo.request-type formdata
   */
  @GetMapping("/withdraw/amount")
  public LotteryWithdrawAmountCO getWithdrawAmount(AppExtCommand cmd, @RequestParam Long activityId) {
    return lotteryActivityRestService.getWithdrawAmount(cmd.getReqUserId(), activityId);
  }

  /**
   * 可提现金额兑换金币.
   *
   * @eo.name 可提现金额兑换金币.
   * @eo.url /exchange
   * @eo.method post
   * @eo.request-type json
   */
  @PostMapping("/exchange")
  public void exchange(@RequestBody @Validated LotteryExchangeGoldCmd cmd) {
    lotteryActivityRestService.exchangeGold(cmd);
  }

  /**
   * 可提现金额转账给指定用户.
   *
   * @eo.name 可提现金额转账给指定用户.
   * @eo.url /transfer
   * @eo.method post
   * @eo.request-type json
   */
  @PostMapping("/transfer")
  public void transfer(@RequestBody @Validated LotteryTransferCmd cmd) {
    lotteryActivityRestService.transfer(cmd);
  }

}
