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

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.seer.teach.common.enums.ResultCodeEnum;
import com.seer.teach.common.exception.CommonException;
import com.seer.teach.mp.app.controller.resp.AgentActivityParentInfoResp;
import com.seer.teach.mp.app.service.IAppAgentActivityParentInfoService;
import com.seer.teach.mp.app.service.IAppAgentService;
import com.seer.teach.mp.entity.MpActivityInfoCollectionEntity;
import com.seer.teach.mp.service.IMpActivityInfoCollectionService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

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

/**
 * <p>
 * 代理商活动参与家长信息服务实现类
 * </p>
 *
 * @author Lingma
 * @since 2025-12-30
 */
@Slf4j
@Service
@AllArgsConstructor
public class AppAgentActivityParentInfoServiceImpl implements IAppAgentActivityParentInfoService {

    private final IMpActivityInfoCollectionService activityInfoCollectionService;
    private final IAppAgentService appAgentService;

    @Override
    public List<AgentActivityParentInfoResp> getParentsByActivityAndAgent(Integer activityId, 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);
        }
        LambdaQueryWrapper<MpActivityInfoCollectionEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(MpActivityInfoCollectionEntity::getActivityId, activityId)
                .eq(MpActivityInfoCollectionEntity::getAgentId, agentId);

        // 查询所有符合条件的记录
        List<MpActivityInfoCollectionEntity> entities = activityInfoCollectionService.list(queryWrapper);

        // 转换为响应对象列表
        return entities.stream()
                .map(this::convertToResp)
                .collect(Collectors.toList());
    }

    private AgentActivityParentInfoResp convertToResp(MpActivityInfoCollectionEntity entity) {
        AgentActivityParentInfoResp resp = new AgentActivityParentInfoResp();
        resp.setId(entity.getId());
        resp.setRelationId(entity.getRelationId());
        resp.setActivityId(entity.getActivityId());
        resp.setAgentId(entity.getAgentId());
        resp.setParentId(entity.getParentId());
        resp.setChildName(entity.getChildName());
        resp.setChildGender(entity.getChildGender());
        resp.setChildBirthDate(entity.getChildBirthDate());
        resp.setGrade(entity.getGrade());
        resp.setSchool(entity.getSchool());
        resp.setRegion(entity.getRegion());
        resp.setParentIdentity(entity.getParentIdentity());
        resp.setLearningSituation(entity.getLearningSituation());
        resp.setStrongSubjectIds(entity.getStrongSubjectIds());
        resp.setWeakSubjectIds(entity.getWeakSubjectIds());
        return resp;
    }
}