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

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.red.circle.framework.dto.PageResult;
import com.red.circle.framework.dto.ResultResponse;
import com.red.circle.other.infra.database.rds.entity.activity.LotteryActivity;
import com.red.circle.other.infra.database.rds.service.activity.LotteryActivityService;
import com.red.circle.other.inner.endpoint.activity.api.LotteryActivityQueryClientApi;
import com.red.circle.other.inner.model.cmd.activity.LotteryActivityQryCmd;
import com.red.circle.other.inner.model.dto.activity.LotteryActivityDTO;
import java.util.List;
import java.util.stream.Collectors;
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 = LotteryActivityQueryClientApi.API_PREFIX, produces = MediaType.APPLICATION_JSON_VALUE)
@RequiredArgsConstructor
public class LotteryActivityQueryClientEndpoint implements LotteryActivityQueryClientApi {

  private final LotteryActivityService lotteryActivityService;

  @Override
  public ResultResponse<PageResult<LotteryActivityDTO>> listActivities(LotteryActivityQryCmd cmd) {
    Integer pageNo = cmd.getPageNo() != null ? cmd.getPageNo() : 1;
    Integer pageSize = cmd.getPageSize() != null ? cmd.getPageSize() : 20;

    Page<LotteryActivity> page = new Page<>(pageNo, pageSize);
    var query = lotteryActivityService.query();

    if (cmd.getActivityName() != null) {
      query.like(LotteryActivity::getActivityName, cmd.getActivityName());
    }
    if (cmd.getActivityCode() != null) {
      query.eq(LotteryActivity::getActivityCode, cmd.getActivityCode());
    }
    if (cmd.getStatus() != null) {
      query.eq(LotteryActivity::getStatus, cmd.getStatus());
    }
    if (cmd.getStartTimeBegin() != null) {
      query.ge(LotteryActivity::getStartTime, cmd.getStartTimeBegin());
    }
    if (cmd.getStartTimeEnd() != null) {
      query.le(LotteryActivity::getStartTime, cmd.getStartTimeEnd());
    }

    IPage<LotteryActivity> result = query.orderByDesc(LotteryActivity::getCreateTime).page(page);

    List<LotteryActivityDTO> dtoList = result.getRecords().stream()
        .map(activity -> {
          LotteryActivityDTO dto = new LotteryActivityDTO();
          BeanUtils.copyProperties(activity, dto);
          return dto;
        })
        .collect(Collectors.toList());

    PageResult<LotteryActivityDTO> pageResult = new PageResult<>();
    pageResult.setCurrent(pageNo);
    pageResult.setSize(pageSize);
    pageResult.setTotal(result.getTotal());
    pageResult.setRecords(dtoList);
    return ResultResponse.success(pageResult);
  }

}
