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

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.seer.teach.common.PageListBean;
import com.seer.teach.common.utils.PageConverterUtils;
import com.seer.teach.mp.admin.controller.req.ActivityFormVariableQueryReq;
import com.seer.teach.mp.admin.controller.resp.AdminActivityFormVariableResp;
import com.seer.teach.mp.admin.convert.AdminActivityFormVariableConvert;
import com.seer.teach.mp.admin.service.IAdminActivityFormVariableService;
import com.seer.teach.mp.entity.MpActivityFormVariableEntity;
import com.seer.teach.mp.service.IMpActivityFormVariableService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

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

/**
 * <p>
 * 管理端活动表单变量服务实现类
 * </p>
 *
 * @author Lingma
 * @since 2026-01-10
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class AdminActivityFormVariableServiceImpl implements IAdminActivityFormVariableService {

    private final IMpActivityFormVariableService activityFormVariableService;

    @Override
    public PageListBean<AdminActivityFormVariableResp> pageList(ActivityFormVariableQueryReq query) {
        Page<MpActivityFormVariableEntity> page = new Page<>(query.getPageNo(), query.getPageSize());
        
        LambdaQueryWrapper<MpActivityFormVariableEntity> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(query.getExecutionId() != null, MpActivityFormVariableEntity::getExecutionId, query.getExecutionId())
               .eq(StringUtils.isNotBlank(query.getVariableCode()), 
                   MpActivityFormVariableEntity::getVariableCode, query.getVariableCode())
               .like(StringUtils.isNotBlank(query.getVariableName()), 
                     MpActivityFormVariableEntity::getVariableName, query.getVariableName())
               .orderByDesc(MpActivityFormVariableEntity::getId);

        Page<MpActivityFormVariableEntity> pageResult = activityFormVariableService.page(page, wrapper);
        List<AdminActivityFormVariableResp> records = pageResult.getRecords().stream()
                .map(AdminActivityFormVariableConvert.INSTANCE::convertToResp)
                .collect(Collectors.toList());

        return PageConverterUtils.convertPageListBean(pageResult, records);
    }

    @Override
    public Boolean saveOrUpdate(ActivityFormVariableQueryReq request) {
        MpActivityFormVariableEntity entity = new MpActivityFormVariableEntity();
        BeanUtil.copyProperties(request, entity);
        
        if (request.getId() == null) {
            // 新增时设置默认值
            entity.setDataType(entity.getDataType() == null ? "string" : entity.getDataType());
        }
        
        return activityFormVariableService.saveOrUpdate(entity);
    }

    @Override
    public Boolean deleteById(Integer id) {
        return activityFormVariableService.removeById(id);
    }

    @Override
    public AdminActivityFormVariableResp getById(Integer id) {
        MpActivityFormVariableEntity entity = activityFormVariableService.getById(id);
        return AdminActivityFormVariableConvert.INSTANCE.convertToResp(entity);
    }

    @Override
    public List<AdminActivityFormVariableResp> listByExecutionId(Integer executionId) {
        if (executionId == null) {
            return java.util.Collections.emptyList();
        }
        List<MpActivityFormVariableEntity> entities = activityFormVariableService.list(
            new LambdaQueryWrapper<MpActivityFormVariableEntity>()
                .eq(MpActivityFormVariableEntity::getExecutionId, executionId)
                .orderByDesc(MpActivityFormVariableEntity::getId)
        );
        
        return entities.stream()
                .map(AdminActivityFormVariableConvert.INSTANCE::convertToResp)
                .collect(Collectors.toList());
    }
}