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

import com.red.circle.mq.business.model.event.gift.GameLuckyGiftBusinessEvent;
import com.red.circle.other.app.common.gift.LuckyGiftGoClient;
import com.red.circle.other.app.listener.GameLuckyGiftBusinessListener;
import com.red.circle.tool.core.text.StringUtils;
import java.util.Objects;
import lombok.RequiredArgsConstructor;
import org.springframework.context.annotation.Profile;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Profile("local")
@RestController
@RequiredArgsConstructor
@RequestMapping(value = "/internal/debug/lucky-gift", produces = MediaType.APPLICATION_JSON_VALUE)
public class LuckyGiftLocalDebugController {

  private final GameLuckyGiftBusinessListener gameLuckyGiftBusinessListener;

  @PostMapping("/business-event")
  public LuckyGiftGoClient.LuckyGiftGoDrawResponse replayBusinessEvent(
      @RequestBody GameLuckyGiftBusinessEvent event) {
    validateEvent(event);

    LuckyGiftGoClient.LuckyGiftGoDrawResponse response =
        gameLuckyGiftBusinessListener.processLotteryEvent(event);
    if (response == null) {
      throw new IllegalStateException("Lucky gift business event returned empty response");
    }
    return response;
  }

  @PostMapping("/go-draw")
  public LuckyGiftGoClient.LuckyGiftGoDrawResponse replayGoDrawOnly(
      @RequestBody GameLuckyGiftBusinessEvent event) {
    validateEvent(event);
    return gameLuckyGiftBusinessListener.drawWithGo(event);
  }

  private void validateEvent(GameLuckyGiftBusinessEvent event) {
    if (StringUtils.isBlank(event.getBusinessId())) {
      throw new IllegalArgumentException("businessId required");
    }
    if (Objects.isNull(event.getUsers()) || event.getUsers().isEmpty()) {
      throw new IllegalArgumentException("users required");
    }
  }
}
