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

import com.red.circle.framework.dto.ResultResponse;
import com.red.circle.other.infra.database.rds.entity.activity.LotteryRecord;
import com.red.circle.other.infra.database.rds.service.activity.LotteryRecordService;
import com.red.circle.other.inner.endpoint.activity.api.LotteryRecordClientApi;
import com.red.circle.other.inner.model.dto.activity.LotteryRecordDTO;
import java.time.LocalDateTime;

import com.red.circle.tool.core.date.TimestampUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.http.MediaType;
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 = LotteryRecordClientApi.API_PREFIX, produces = MediaType.APPLICATION_JSON_VALUE)
@RequiredArgsConstructor
public class LotteryRecordClientEndpoint implements LotteryRecordClientApi {

  private final LotteryRecordService lotteryRecordService;

  @Override
  public ResultResponse<Long> saveRecord(String recordNo, Long userId, Long activityId,
                                          Long prizeId, String prizeName, String prizeType,
                                          Integer isWin) {
    LotteryRecord record = new LotteryRecord();
    record.setRecordNo(recordNo);
    record.setUserId(userId);
    record.setActivityId(activityId);
    record.setPrizeId(prizeId);
    record.setPrizeName(prizeName);
    record.setPrizeType(prizeType);
    record.setIsWin(isWin);
    record.setDrawTime(TimestampUtils.now());
    record.setPrizeStatus(isWin == 1 ? 0 : null);
    
    lotteryRecordService.save(record);
    
    return ResultResponse.success(record.getId());
  }

  @Override
  public ResultResponse<LotteryRecordDTO> getByRecordNo(String recordNo) {
    LotteryRecord record = lotteryRecordService.query()
        .eq(LotteryRecord::getRecordNo, recordNo)
        .getOne();
    
    if (record == null) {
      return ResultResponse.success(null);
    }
    
    LotteryRecordDTO dto = new LotteryRecordDTO();
    BeanUtils.copyProperties(record, dto);
    
    return ResultResponse.success(dto);
  }

}
