package com.seer.teach.mp.app.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.seer.teach.common.enums.ResultCodeEnum;
import com.seer.teach.common.exception.CommonException;
import com.seer.teach.mp.app.controller.resp.AgentActivityParticipantResp;
import com.seer.teach.mp.app.service.IAppAgentActivityParticipantService;
import com.seer.teach.mp.app.service.IAppAgentService;
import com.seer.teach.mp.entity.MpActivityEntity;
import com.seer.teach.mp.entity.MpAgentActivityParticipantEntity;
import com.seer.teach.mp.service.IMpActivityService;
import com.seer.teach.mp.service.IMpAgentActivityParticipantService;
import com.seer.teach.user.api.UserInfoServiceApi;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * <p>
 * APP端代理商活动参与记录服务实现类
 * </p>
 *
 * @author Lingma
 * @since 2025-12-30
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class AppAgentActivityParticipantServiceImpl implements IAppAgentActivityParticipantService {

    private final IMpAgentActivityParticipantService agentActivityParticipantService;

    private final UserInfoServiceApi userInfoServiceApi;

    private final IAppAgentService appAgentService;

    private final IMpActivityService mpActivityService;

    @Override
    public List<AgentActivityParticipantResp> getParticipantsByActivityAndAgent(Integer agentId, Integer userId) {
        var userAgentId = appAgentService.getAgentIdByUserId(userId);
        if (Objects.isNull(userAgentId)) {
            return List.of();
        }
        log.info("userAgentId:{}", userAgentId);
        if(userAgentId.intValue() != agentId){
            throw new CommonException(ResultCodeEnum.RELATION_NOT_FOUND);
        }
        var participants = agentActivityParticipantService.getListByAgentId(agentId);

        if (CollectionUtil.isEmpty(participants)) {
            return List.of();
        }
        Set<Integer> activityIds = participants.stream().map(MpAgentActivityParticipantEntity::getActivityId).collect(Collectors.toSet());

        List<MpActivityEntity> parentInfos = mpActivityService.listByIds(activityIds);

        var activityInfoMap = parentInfos.stream().collect(Collectors.toMap(MpActivityEntity::getId, activity -> activity));

        return participants.stream()
            .map(entity -> convertToDto(entity, activityInfoMap))
            .collect(Collectors.toList());
    }


    private AgentActivityParticipantResp convertToDto(MpAgentActivityParticipantEntity entity, Map<Integer, MpActivityEntity> activityInfoMap) {
        AgentActivityParticipantResp resp = new AgentActivityParticipantResp();
        resp.setId(entity.getId());
        resp.setActivityId(entity.getActivityId());
        resp.setAgentId(entity.getAgentId());
        MpActivityEntity activity = activityInfoMap.get(entity.getActivityId());
        if(Objects.nonNull(activity)){
            resp.setActivityName(activity.getActivityName());
        }
        return resp;
    }
}