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

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.seer.teach.common.enums.ResultCodeEnum;
import com.seer.teach.common.utils.AssertUtils;
import com.seer.teach.mp.app.controller.req.AppMpSignUpActivityReq;
import com.seer.teach.mp.app.controller.resp.AppMpSignUpActivityResp;
import com.seer.teach.mp.app.convert.AppMpActivityInfoCollectionConvert;
import com.seer.teach.mp.entity.MpActivityEntity;
import com.seer.teach.mp.entity.MpActivityInfoCollectionEntity;
import com.seer.teach.mp.entity.MpAgentActivityParticipantEntity;
import com.seer.teach.mp.entity.MpAgentEntity;
import com.seer.teach.mp.entity.MpParentAgentActivityRelationEntity;
import com.seer.teach.mp.service.IMpActivityInfoCollectionService;
import com.seer.teach.mp.service.IMpActivityService;
import com.seer.teach.mp.service.IMpAgentActivityParticipantService;
import com.seer.teach.mp.service.IMpAgentService;
import com.seer.teach.mp.service.IMpParentAgentActivityRelationService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Objects;

@RequiredArgsConstructor
@Slf4j
@Service
public class AppParentAgentActivityService {

    private final IMpParentAgentActivityRelationService parentAgentActivityRelationService;
    private final IMpActivityInfoCollectionService activityInfoCollectionService;
    private final IMpActivityService activityService;
    private final IMpAgentService agentService;
    private final IMpAgentActivityParticipantService mpAgentActivityParticipantService;

    /**
     * 报名活动
     *
     * @param request 报名信息
     * @param parentId 家长ID
     * @return true表示成功，false表示失败
     */
    public boolean signUpForActivityWithInfo(AppMpSignUpActivityReq request, Integer parentId) {
        Integer agentId = request.getAgentId();
        Integer activityId = request.getActivityId();
        MpActivityEntity activity = activityService.getById(activityId);
        AssertUtils.notNull(activity, ResultCodeEnum.INVALID_ACTIVITY);

        MpAgentEntity agent = agentService.getById(agentId);
        AssertUtils.notNull(agent, ResultCodeEnum.INVALID_AGENT);

        MpAgentActivityParticipantEntity participants = mpAgentActivityParticipantService.getParticipantsByActivityAndAgent(request.getActivityId(), request.getAgentId());
        AssertUtils.notNull(participants, ResultCodeEnum.AGENT_NON_PARTICIPANT_ACTIVE);

        MpParentAgentActivityRelationEntity relation = parentAgentActivityRelationService.getByAgentIdAndActivityIdAndParentId(agentId, activityId, parentId);
        AssertUtils.isNull(relation, ResultCodeEnum.PARENT_ALREADY_SIGN_UP);

        relation = new MpParentAgentActivityRelationEntity();
        relation.setActivityId(activityId);
        relation.setAgentId(agentId);
        relation.setParentId(parentId);
        relation.setActivityName(activity.getActivityName());
        relation.setAgentName(agent.getAgentName());
        relation.setStatus(1);
        relation.setSignUpTime(LocalDateTime.now());
        boolean saved = parentAgentActivityRelationService.save(relation);
        log.info("报名结果：{}，关系ID：{}", saved, relation.getId());

        if(saved){
            MpActivityInfoCollectionEntity mpActivityInfoCollectionEntity = AppMpActivityInfoCollectionConvert.INSTANCE.convert2Entity(request);
            mpActivityInfoCollectionEntity.setRelationId(relation.getId());
            boolean activityInfoCollectionResult = activityInfoCollectionService.save(mpActivityInfoCollectionEntity);
            log.info("活动信息收集结果：{}", activityInfoCollectionResult);
            return activityInfoCollectionResult;
        }
        return false;
    }

    /**
     * 更新活动信息
     *
     * @param request 活动信息
     * @param parentId 家长ID
     * @return true表示成功，false表示失败
     */
    public boolean updateActivityInfo(AppMpSignUpActivityReq request, Integer parentId) {
        Integer agentId = request.getAgentId();
        Integer activityId = request.getActivityId();
        MpParentAgentActivityRelationEntity relation = parentAgentActivityRelationService.getByAgentIdAndActivityIdAndParentId(agentId, activityId, parentId);
        AssertUtils.notNull(relation, ResultCodeEnum.PARENT_NOT_SIGNED_UP);

        MpActivityInfoCollectionEntity mpActivityInfoCollectionEntity = AppMpActivityInfoCollectionConvert.INSTANCE.convert2Entity(request);
        mpActivityInfoCollectionEntity.setRelationId(relation.getId());
        return activityInfoCollectionService.updateById(mpActivityInfoCollectionEntity);
    }

    /**
     * 取消报名
     *
     * @param relationId 关系ID
     * @param parentId   家长ID
     * @return true表示成功，false表示失败
     */
    public boolean cancelSignUp(Integer relationId, Integer parentId) {
        MpParentAgentActivityRelationEntity relation = parentAgentActivityRelationService.getById(relationId);
        if (Objects.nonNull(relation) && relation.getParentId().equals(parentId)) {
            relation.setStatus(0);
            boolean result = parentAgentActivityRelationService.updateById(relation);
            log.info("取消报名结果：{}", result);
            return result;
        }
        return false;
    }


    /**
     * 获取报名信息
     *
     * @param agentId    代理商ID
     * @param activityId 活动ID
     * @param parentId   家长ID
     * @return 报名信息
     */
    public AppMpSignUpActivityResp getByActivityIdAndParentId(Integer agentId, Integer activityId, Integer parentId) {
        LambdaQueryWrapper<MpParentAgentActivityRelationEntity> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(MpParentAgentActivityRelationEntity::getActivityId, activityId)
                .eq(MpParentAgentActivityRelationEntity::getParentId, parentId)
                .eq(MpParentAgentActivityRelationEntity::getAgentId, agentId);
        MpParentAgentActivityRelationEntity one = parentAgentActivityRelationService.getOne(wrapper);
        AssertUtils.notNull(one, ResultCodeEnum.INVALID_ACTIVITY);
        MpActivityInfoCollectionEntity activityInfoCollection = activityInfoCollectionService.getByActivityIdAndParentId(agentId, activityId, parentId);
        return AppMpActivityInfoCollectionConvert.INSTANCE.convert2Resp(activityInfoCollection);
    }

}