package com.jeesharp.business.modules.member.application;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.jeesharp.business.modules.member.convert.MemberPointsLogConvert;
import com.jeesharp.business.modules.member.entity.MemberPointsLog;
import com.jeesharp.business.modules.member.service.MemberPointsLogService;
import com.jeesharp.business.modules.member.service.MemberService;
import com.jeesharp.business.modules.member.vo.MemberPointsLogExportVO;
import com.jeesharp.business.modules.member.vo.MemberPointsLogInfoVO;
import com.jeesharp.business.modules.member.vo.MemberPointsLogListVO;
import com.jeesharp.business.modules.member.vo.MemberPointsLogQueryVO;
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-24
 */
@Service
@AllArgsConstructor
@Transactional(readOnly = true)
public class MemberPointsLogManageApplicationService {

    private final MemberPointsLogService memberPointsLogService;
    private final MemberService memberService;

    public PageRespVO<MemberPointsLogListVO> findPage(Page<MemberPointsLog> page, MemberPointsLogQueryVO query) {
        MemberPointsLogQueryVO prepared = prepareQuery(query);
        if (prepared == null) {
            return emptyPage(page);
        }
        Page<MemberPointsLog> result = memberPointsLogService.findPage(page, prepared);
        List<MemberPointsLogListVO> rows = MemberPointsLogConvert.INSTANCE.toListVO(result.getRecords());
        if (CollUtil.isNotEmpty(rows)) {
            memberService.enrichMemberDisplay(rows);
        }
        PageRespVO<MemberPointsLogListVO> pageVo = new PageRespVO<>(result);
        pageVo.setRecords(rows);
        return pageVo;
    }

    public MemberPointsLogInfoVO get(String id) {
        MemberPointsLog entity = memberPointsLogService.get(id);
        if (entity == null) {
            throw new BizErrorException("积分流水不存在");
        }
        MemberPointsLogInfoVO vo = MemberPointsLogConvert.INSTANCE.toInfoVO(entity);
        memberService.enrichMemberDisplay(vo);
        return vo;
    }

    public List<MemberPointsLogExportVO> findList(MemberPointsLogQueryVO query) {
        MemberPointsLogQueryVO prepared = prepareQuery(query);
        if (prepared == null) {
            return Collections.emptyList();
        }
        List<MemberPointsLog> list = memberPointsLogService.findList(prepared);
        List<MemberPointsLogExportVO> exportList = MemberPointsLogConvert.INSTANCE.toExportVOList(list);
        enrichExportMemberDisplay(list, exportList);
        return exportList;
    }

    private void enrichExportMemberDisplay(List<MemberPointsLog> entities, List<MemberPointsLogExportVO> exportList) {
        if (CollUtil.isEmpty(entities) || CollUtil.isEmpty(exportList) || entities.size() != exportList.size()) {
            return;
        }
        List<MemberPointsLogListVO> displayRows = entities.stream()
                .map(MemberPointsLogConvert.INSTANCE::toListSingleVO)
                .toList();
        memberService.enrichMemberDisplay(displayRows);
        for (int i = 0; i < exportList.size(); i++) {
            MemberPointsLogListVO row = displayRows.get(i);
            MemberPointsLogExportVO exp = exportList.get(i);
            exp.setMemberNo(row.getMemberNo());
            exp.setMemberNickname(row.getMemberNickname());
            exp.setMemberMobile(row.getMemberMobile());
        }
    }

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

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