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

import com.red.circle.common.business.core.enums.SysOriginPlatformEnum;
import com.red.circle.common.business.enums.SendPropsOrigin;
import com.red.circle.console.app.scheduler.StatisticsOperationActiveIndexTask;
import com.red.circle.console.infra.database.rds.service.count.StatisticsDailyRegisterLogoutPlatformService;
import com.red.circle.console.infra.database.util.DateFormatEnum;
import com.red.circle.console.infra.database.util.LocalDateUtils;
import com.red.circle.console.infra.database.util.ZonedDateTimeUtils;
import com.red.circle.framework.core.dto.CommonCommand;
import com.red.circle.framework.web.controller.BaseController;
import com.red.circle.other.inner.endpoint.activity.PropsActivityClient;
import com.red.circle.other.inner.model.cmd.activity.SendActivityRewardCmd;
import com.red.circle.tool.core.json.JacksonUtils;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;

/**
 * @author pengliang on 2023/10/18
 */
@Slf4j
@RestController
@RequiredArgsConstructor
@RequestMapping(value = "/open-console-test", produces = MediaType.APPLICATION_JSON_VALUE)
public class ConsoleTestRestController extends BaseController {

  private final StatisticsOperationActiveIndexTask statisticsOperationActiveIndexTask;
  private final PropsActivityClient propsActivityClient;
  private final StatisticsDailyRegisterLogoutPlatformService statisticsDailyRegisterLogoutPlatformService;

  @GetMapping("/ping")
  public CommonCommand ping(CommonCommand cmd) {
    log.warn("test:{}", JacksonUtils.toJson(cmd));
    return cmd;
  }

  @PostMapping("/statisticsDaily")
  public void statisticsDaily() {
    statisticsOperationActiveIndexTask.statisticsDaily();
  }

  /**
   * 发放活动奖励.
   */
  @PostMapping("/send-reward")
  public void sendReward(@RequestParam String type, @RequestParam String activityId) {

    var countTime = countDateAsiaShanghai();
    var countNumber = Integer.parseInt(
            LocalDateUtils.format(countTime, DateFormatEnum.yyyyMMdd));
    var now = nowDateAsiaShanghai();

    log.warn("[userAddedCount] 用户新增情况统计,开始{},{},{}", countNumber, countTime, now);
    // 用户新增情况统计
    statisticsDailyRegisterLogoutPlatformService.countUserAdded(
            countNumber,
            countTime,
            now
    );
    log.warn("[userAddedCount] 用户新增情况统计,完成");
    /*propsActivityClient.sendActivityReward(new SendActivityRewardCmd()
            .setTrackId(activityId)
            .setOrigin(SendPropsOrigin.ACTIVITY_REWARD)
            .setSysOrigin(SysOriginPlatformEnum.valueOf(sysOrigin))
            .setSourceGroupId(rank.getResourceGroupId())
            .setAcceptUserId(acceptUser.getUserId())
    );*/

  }

  private LocalDate countDateAsiaShanghai() {
    return ZonedDateTimeUtils.nowAsiaShanghai()
            .minusDays(1)
            .toLocalDate();
  }

  private LocalDate nowDateAsiaShanghai() {
    return ZonedDateTimeUtils.nowAsiaShanghai().toLocalDate();
  }
}
