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

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.seer.teach.mp.admin.controller.resp.AdminActivityFormRelationResp;
import com.seer.teach.mp.admin.convert.AdminMpActivityFormRelationConvert;
import com.seer.teach.mp.admin.service.IAdminActivityFormRelationService;
import com.seer.teach.mp.entity.MpActivityFormEntity;
import com.seer.teach.mp.entity.MpActivityFormRelationEntity;
import com.seer.teach.mp.service.IMpActivityFormRelationService;
import com.seer.teach.mp.service.IMpActivityFormService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * <p>
 * 管理端活动与表单模板关联服务实现类
 * </p>
 *
 * @author Lingma
 * @since 2026-01-10
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class AdminActivityFormRelationServiceImpl implements IAdminActivityFormRelationService {

    private final IMpActivityFormRelationService activityFormTemplateRelationService;
    private final IMpActivityFormService activityFormTemplateService;

    @Override
    public Boolean associateFormWithActivity(Integer activityId, Integer formId) {
        return activityFormTemplateRelationService.createRelation(activityId, formId);
    }

    @Override
    public Boolean disassociateFormWithActivity(Integer activityId, Integer formId) {
        return activityFormTemplateRelationService.remove(
            new LambdaQueryWrapper<MpActivityFormRelationEntity>()
                .eq(MpActivityFormRelationEntity::getActivityId, activityId)
                .eq(MpActivityFormRelationEntity::getFormId, formId)
        );
    }

    @Override
    public List<AdminActivityFormRelationResp> getFormsByActivity(Integer activityId) {
        if (activityId == null) {
            return java.util.Collections.emptyList();
        }
        List<MpActivityFormRelationEntity> relations = activityFormTemplateRelationService.list(
            new LambdaQueryWrapper<MpActivityFormRelationEntity>()
                .eq(MpActivityFormRelationEntity::getActivityId, activityId)
        );
        if(CollectionUtil.isEmpty(relations)){
            return Collections.emptyList();
        }
        List<Integer> formIds = relations.stream()
            .map(MpActivityFormRelationEntity::getFormId)
            .collect(java.util.stream.Collectors.toList());

        List<MpActivityFormEntity> forms = activityFormTemplateService.list(
            new LambdaQueryWrapper<MpActivityFormEntity>()
                .in(MpActivityFormEntity::getId, formIds)
        );
        log.info("getFormsByActivity: {}", forms);
        if(CollectionUtil.isEmpty(forms)){
            return Collections.emptyList();
        }
        Map<Integer, MpActivityFormEntity> mapping = forms.stream().collect(Collectors.toMap(MpActivityFormEntity::getId, Function.identity(), (old, newOne) -> newOne));

        List<AdminActivityFormRelationResp> adminActivityFormRelationRespList = AdminMpActivityFormRelationConvert.INSTANCE.toAdminActivityFormRelationRespList(relations);
        for (AdminActivityFormRelationResp adminActivityFormRelationResp : adminActivityFormRelationRespList) {
            MpActivityFormEntity activityForm = mapping.get(adminActivityFormRelationResp.getFormId());
            if(Objects.nonNull(activityForm)){
                adminActivityFormRelationResp.setFormName(activityForm.getFormName());
                adminActivityFormRelationResp.setFromDescription(activityForm.getFormDescription());
            }
         }
        log.debug("getFormsByActivity: {}", adminActivityFormRelationRespList);
       return adminActivityFormRelationRespList;
    }
}