package com.jeesharp.business.modules.marketing.couponVerifyLog.application;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.jeesharp.business.modules.marketing.couponVerifyLog.convert.MemberCouponVerifyLogConvert;
import com.jeesharp.business.modules.marketing.couponVerifyLog.entity.MemberCouponVerifyLog;
import com.jeesharp.business.modules.marketing.couponVerifyLog.service.MemberCouponVerifyLogService;
import com.jeesharp.business.modules.marketing.couponVerifyLog.vo.MemberCouponVerifyLogExportVO;
import com.jeesharp.business.modules.marketing.couponVerifyLog.vo.MemberCouponVerifyLogInfoVO;
import com.jeesharp.business.modules.marketing.couponVerifyLog.vo.MemberCouponVerifyLogListVO;
import com.jeesharp.business.modules.marketing.couponVerifyLog.vo.MemberCouponVerifyLogQueryVO;
import com.jeesharp.business.modules.member.service.MemberService;
import com.jeesharp.framework.exception.BizErrorException;
import com.jeesharp.framework.pojo.Page;
import com.jeesharp.framework.pojo.query.PageRespVO;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collections;
import java.util.List;

/**
 * 核销流水后台应用服务：分页、详情、导出与会员展示补全。
 *
 * @author jeesharp
 * @since 2026-06-25
 */
@Service
@AllArgsConstructor
@Transactional(readOnly = true)
public class MemberCouponVerifyLogApplicationService {

    private final MemberCouponVerifyLogService memberCouponVerifyLogService;
    private final MemberService memberService;

    public MemberCouponVerifyLogInfoVO get(String id) {
        MemberCouponVerifyLog entity = memberCouponVerifyLogService.get(id);
        if (entity == null) {
            throw new BizErrorException("核销记录不存在");
        }
        MemberCouponVerifyLogInfoVO vo = MemberCouponVerifyLogConvert.INSTANCE.toInfoVO(entity);
        memberService.enrichMemberDisplay(vo);
        return vo;
    }

    public PageRespVO<MemberCouponVerifyLogListVO> findPage(Page<MemberCouponVerifyLog> page, MemberCouponVerifyLogQueryVO query) {
        MemberCouponVerifyLogQueryVO prepared = prepareQuery(query);
        if (prepared == null) {
            return emptyPage(page);
        }
        Page<MemberCouponVerifyLog> result = memberCouponVerifyLogService.findPage(page, prepared);
        PageRespVO<MemberCouponVerifyLogListVO> pageVo = MemberCouponVerifyLogConvert.INSTANCE.toPageRespVO(result);
        memberService.enrichMemberDisplay(pageVo.getRecords());
        return pageVo;
    }

    public List<MemberCouponVerifyLogExportVO> findList(MemberCouponVerifyLogQueryVO query) {
        MemberCouponVerifyLogQueryVO prepared = prepareQuery(query);
        if (prepared == null) {
            return Collections.emptyList();
        }
        List<MemberCouponVerifyLog> list = memberCouponVerifyLogService.findList(prepared);
        List<MemberCouponVerifyLogExportVO> exportList = MemberCouponVerifyLogConvert.INSTANCE.toExportVOList(list);
        memberService.enrichMemberDisplay(exportList);
        return exportList;
    }

    private MemberCouponVerifyLogQueryVO prepareQuery(MemberCouponVerifyLogQueryVO query) {
        MemberCouponVerifyLogQueryVO q = query != null ? query : new MemberCouponVerifyLogQueryVO();
        if (StrUtil.isNotBlank(q.getMemberKeyword())) {
            List<String> memberIds = memberService.findIdsByKeyword(q.getMemberKeyword().trim());
            if (CollUtil.isEmpty(memberIds)) {
                return null;
            }
            q.setMemberIdIn(memberIds);
        }
        return q;
    }

    private PageRespVO<MemberCouponVerifyLogListVO> emptyPage(Page<MemberCouponVerifyLog> page) {
        PageRespVO<MemberCouponVerifyLogListVO> empty = new PageRespVO<>();
        empty.setTotal(0L);
        empty.setPageNo(page.getPageNo());
        empty.setPageSize(page.getPageSize());
        empty.setRecords(Collections.emptyList());
        return empty;
    }
}
