package com.red.circle.other.app.inner.endpoint.activity;

import com.red.circle.framework.core.asserts.ResponseAssert;
import com.red.circle.framework.dto.ResultResponse;
import com.red.circle.other.infra.database.rds.entity.activity.LotteryTicket;
import com.red.circle.other.infra.database.rds.entity.activity.LotteryTicketRecord;
import com.red.circle.other.infra.database.rds.service.activity.LotteryTicketRecordService;
import com.red.circle.other.infra.database.rds.service.activity.LotteryTicketService;
import com.red.circle.other.inner.asserts.lottery.LotteryErrorCode;
import com.red.circle.other.inner.endpoint.activity.api.LotteryTicketManageClientApi;
import com.red.circle.other.inner.model.cmd.activity.LotteryTicketGrantCmd;
import java.sql.Timestamp;
import lombok.RequiredArgsConstructor;
import org.springframework.http.MediaType;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 抽奖券管理ClientEndpoint.
 *
 * @author system
 * @since 2025-10-18
 */
@Validated
@RestController
@RequestMapping(value = LotteryTicketManageClientApi.API_PREFIX, produces = MediaType.APPLICATION_JSON_VALUE)
@RequiredArgsConstructor
public class LotteryTicketManageClientEndpoint implements LotteryTicketManageClientApi {

  private final LotteryTicketService lotteryTicketService;
  private final LotteryTicketRecordService lotteryTicketRecordService;

  @Override
  @Transactional(rollbackFor = Exception.class)
  public ResultResponse<Void> grantTickets(LotteryTicketGrantCmd cmd) {
    // 校验必填参数
    ResponseAssert.isTrue(LotteryErrorCode.USER_ID_REQUIRED, 
        cmd.getUserIds() != null && !cmd.getUserIds().isEmpty());
    ResponseAssert.isTrue(LotteryErrorCode.GRANT_COUNT_INVALID, 
        cmd.getCount() != null && cmd.getCount() > 0);
    
    // 来源ID必填 - 用于追踪抽奖券的发放来源
    ResponseAssert.isTrue(LotteryErrorCode.ACTIVITY_ID_REQUIRED, 
        cmd.getSourceId() != null && !cmd.getSourceId().isEmpty());
    
    String ticketSource = cmd.getTicketSource() != null ? cmd.getTicketSource() : "SYSTEM";
    Timestamp now = new Timestamp(System.currentTimeMillis());
    
    for (Long userId : cmd.getUserIds()) {
      // 创建抽奖券记录
      LotteryTicket ticket = new LotteryTicket();
      ticket.setUserId(userId);
      ticket.setTicketSource(ticketSource);
      ticket.setSourceId(cmd.getSourceId());
      ticket.setTotalCount(cmd.getCount());
      ticket.setUsedCount(0);
      ticket.setRemainingCount(cmd.getCount());
      ticket.setExpireTime(cmd.getExpireTime());
      ticket.setStatus(1); // 正常状态
      lotteryTicketService.save(ticket);

      // 记录发放日志
      LotteryTicketRecord ticketRecord = new LotteryTicketRecord();
      ticketRecord.setUserId(userId);
      ticketRecord.setTicketId(ticket.getId());
      ticketRecord.setChangeCount(cmd.getCount());
      ticketRecord.setChangeType(1); // 1-发放
      ticketRecord.setRemark(cmd.getRemark() != null ? cmd.getRemark() : "系统发放抽奖券");
      lotteryTicketRecordService.save(ticketRecord);
    }
    
    return ResultResponse.success();
  }

}
