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

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.jeesharp.business.modules.setting.convert.MarketingActivityScopeConvert;
import com.jeesharp.business.modules.setting.dto.MarketingActivityScopeSaveDTO;
import com.jeesharp.business.modules.setting.entity.MarketingActivityScope;
import com.jeesharp.business.modules.setting.service.MarketingActivityScopeService;
import com.jeesharp.business.modules.setting.service.MarketingActivityService;
import com.jeesharp.business.modules.setting.vo.MarketingActivityScopeListVO;
import com.jeesharp.business.modules.setting.vo.MarketingActivityScopeQueryVO;
import com.jeesharp.business.modules.setting.vo.MarketingActivityScopeVO;
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;
import java.util.Map;

/**
 * 活动营销投放范围应用服务：后台范围查询、维护及活动名称展示编排。
 *
 * @author jeesharp
 * @since 2026-06-04
 */
@Service
@AllArgsConstructor
@Transactional(readOnly = true)
public class MarketingActivityScopeApplicationService {

    private final MarketingActivityScopeService marketingActivityScopeService;
    private final MarketingActivityService marketingActivityService;

    public MarketingActivityScopeVO get(String id) {
        MarketingActivityScope entity = marketingActivityScopeService.get(id);
        if (entity == null) {
            return null;
        }
        MarketingActivityScopeVO vo = MarketingActivityScopeConvert.INSTANCE.toInfoVO(entity);
        enrichInfoDisplay(vo);
        return vo;
    }

    public PageRespVO<MarketingActivityScopeListVO> findPage(Page<MarketingActivityScope> page, MarketingActivityScopeQueryVO query) {
        MarketingActivityScopeQueryVO prepared = prepareQuery(query);
        if (prepared == null) {
            return emptyPage(page);
        }
        Page<MarketingActivityScope> result = marketingActivityScopeService.findPage(page, prepared);
        PageRespVO<MarketingActivityScopeListVO> pageVo = MarketingActivityScopeConvert.INSTANCE.toPageRespVO(result);
        enrichListDisplay(pageVo.getRecords());
        return pageVo;
    }

    @Transactional(readOnly = false)
    public void save(MarketingActivityScopeSaveDTO dto) {
        MarketingActivityScope entity = MarketingActivityScopeConvert.INSTANCE.toEntity(dto);
        if (StrUtil.isBlank(dto.getId())) {
            marketingActivityScopeService.createMarketingActivityScope(entity);
            return;
        }
        marketingActivityScopeService.updateMarketingActivityScope(entity);
    }

    @Transactional(readOnly = false)
    public void delete(String ids) {
        marketingActivityScopeService.batchDelete(StrUtil.splitTrim(ids, ','));
    }

    /**
     * 管理端活动名称/编码筛选 → activityIdIn。
     */
    private MarketingActivityScopeQueryVO prepareQuery(MarketingActivityScopeQueryVO query) {
        MarketingActivityScopeQueryVO q = query != null ? query : new MarketingActivityScopeQueryVO();
        if (StrUtil.isNotBlank(q.getActivityName())) {
            List<String> activityIds = marketingActivityService.findIdsByNameLike(q.getActivityName().trim());
            q.setActivityName(null);
            if (CollUtil.isEmpty(activityIds)) {
                return null;
            }
            q.setActivityIdIn(activityIds);
        }
        return q;
    }

    private void enrichListDisplay(List<MarketingActivityScopeListVO> rows) {
        if (CollUtil.isEmpty(rows)) {
            return;
        }
        List<String> activityIds = rows.stream()
            .map(MarketingActivityScopeListVO::getActivityId)
            .filter(StrUtil::isNotBlank)
            .distinct()
            .toList();
        Map<String, String> activityNameMap = marketingActivityService.mapActivityNameByIds(activityIds);
        for (MarketingActivityScopeListVO row : rows) {
            if (StrUtil.isNotBlank(row.getActivityId())) {
                row.setActivityName(activityNameMap.get(row.getActivityId()));
            }
        }
    }

    private void enrichInfoDisplay(MarketingActivityScopeVO vo) {
        if (vo == null || StrUtil.isBlank(vo.getActivityId())) {
            return;
        }
        Map<String, String> nameMap = marketingActivityService.mapActivityNameByIds(List.of(vo.getActivityId()));
        vo.setActivityName(nameMap.get(vo.getActivityId()));
    }

    private static PageRespVO<MarketingActivityScopeListVO> emptyPage(Page<MarketingActivityScope> page) {
        PageRespVO<MarketingActivityScopeListVO> empty = new PageRespVO<>();
        empty.setRecords(Collections.emptyList());
        empty.setTotal(0L);
        empty.setPageNo(page != null ? page.getPageNo() : 1L);
        empty.setPageSize(page != null ? page.getPageSize() : 10L);
        return empty;
    }
}
