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

import com.red.circle.framework.dto.PageResult;
import com.red.circle.framework.web.controller.BaseController;
import com.red.circle.other.infra.convertor.message.PublicMessageConvertor;
import com.red.circle.other.inner.model.dto.message.PublicMessageDTO;
import com.red.circle.other.adapter.app.message.dto.PublicMessagePageQry;
import com.red.circle.other.infra.database.mongo.entity.message.PublicMessage;
import com.red.circle.other.infra.database.mongo.service.message.PublicMessageService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Page;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 公共消息控制器.
 * </p>
 *
 * @author your_name
 * @since 2025-10-14
 * @eo.api-type http
 * @eo.groupName 消息管理
 * @eo.path /public-message
 */
@Slf4j
@RequiredArgsConstructor
@RestController
@RequestMapping(value = "/public-message", produces = MediaType.APPLICATION_JSON_VALUE)
public class PublicMessageController extends BaseController {

  private final PublicMessageService publicMessageService;
  private final PublicMessageConvertor publicMessageConvertor;

  /**
   * 分页查询公共消息列表.
   *
   * @param qry 查询条件
   * @return 分页结果
   * @eo.name 分页查询公共消息列表
   * @eo.url /page
   * @eo.method get
   * @eo.request-type formdata
   */
  @GetMapping("/page")
  public PageResult<PublicMessageDTO> pagePublicMessages(@Validated PublicMessagePageQry qry) {
    log.info("分页查询公共消息, qry={}", qry);

    // 调用服务层分页查询
    Page<PublicMessage> page = publicMessageService.pagePublicMessages(
        qry.getType(),
        qry.getPageNo(),
        qry.getPageSize()
    );

    // 转换为DTO
    List<PublicMessageDTO> dtoList = page.getContent().stream()
        .map(publicMessageConvertor::toDTO)
        .collect(Collectors.toList());

    PageResult<PublicMessageDTO> pageResult = new PageResult<>();
    pageResult.setSize(page.getSize());

    pageResult.setCurrent(qry.getPageNo());
    pageResult.setRecords(dtoList);
    pageResult.setTotal(page.getTotalElements());

    // 构建分页结果
    return pageResult;
  }

  /**
   * 查询最新的公共消息列表.
   *
   * @param qry 查询条件
   * @return 消息列表
   * @eo.name 查询最新的公共消息列表
   * @eo.url /latest
   * @eo.method get
   * @eo.request-type formdata
   */
//  @GetMapping("/latest")
  public List<PublicMessageDTO> listLatestMessages(@Validated PublicMessagePageQry qry) {
    log.info("查询最新公共消息, qry={}", qry);

    // 如果没有传limit，默认查询10条
    Integer limit = qry.getPageSize() != null ? qry.getPageSize() : 10;

    // 调用服务层查询
    List<PublicMessage> messages = publicMessageService.listLatestMessages(
        qry.getType(),
        limit
    );

    // 转换为DTO
    return messages.stream()
        .map(publicMessageConvertor::toDTO)
        .collect(Collectors.toList());
  }

}
