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.ActivityFormFieldQueryReq;
import com.seer.teach.mp.admin.controller.resp.AdminActivityFormFieldResp;
import com.seer.teach.mp.admin.convert.AdminActivityFormFieldConvert;
import com.seer.teach.mp.admin.service.IAdminActivityFormFieldService;
import com.seer.teach.mp.entity.MpActivityFormFieldEntity;
import com.seer.teach.mp.service.IMpActivityFormFieldService;
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 AdminActivityFormFieldServiceImpl implements IAdminActivityFormFieldService {

    private final IMpActivityFormFieldService activityFormFieldService;

    @Override
    public PageListBean<AdminActivityFormFieldResp> pageList(ActivityFormFieldQueryReq query) {
        Page<MpActivityFormFieldEntity> page = new Page<>(query.getPageNo(), query.getPageSize());
        
        LambdaQueryWrapper<MpActivityFormFieldEntity> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(query.getFormId() != null, MpActivityFormFieldEntity::getFormId, query.getFormId())
               .like(StringUtils.isNotBlank(query.getFieldName()), 
                     MpActivityFormFieldEntity::getFieldName, query.getFieldName())
               .eq(StringUtils.isNotBlank(query.getFieldType()), 
                   MpActivityFormFieldEntity::getFieldType, query.getFieldType())
               .orderByAsc(MpActivityFormFieldEntity::getSortOrder)
               .orderByDesc(MpActivityFormFieldEntity::getId);

        Page<MpActivityFormFieldEntity> pageResult = activityFormFieldService.page(page, wrapper);

        return PageConverterUtils.convertPageList(pageResult, AdminActivityFormFieldConvert.INSTANCE::convertToResp);
    }

    @Override
    public Boolean saveOrUpdate(ActivityFormFieldQueryReq request) {
        MpActivityFormFieldEntity entity = new MpActivityFormFieldEntity();
        BeanUtil.copyProperties(request, entity);
        
        if (request.getId() == null) {
            // 新增时设置默认值
            entity.setIsRequired(entity.getIsRequired() == null ? 0 : entity.getIsRequired());
            entity.setSortOrder(entity.getSortOrder() == null ? 0 : entity.getSortOrder());
        }
        
        return activityFormFieldService.saveOrUpdate(entity);
    }

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

    @Override
    public AdminActivityFormFieldResp getById(Integer id) {
        if (id == null) {
            return null;
        }
        MpActivityFormFieldEntity entity = activityFormFieldService.getById(id);
        return AdminActivityFormFieldConvert.INSTANCE.convertToResp(entity);
    }

    @Override
    public List<AdminActivityFormFieldResp> listByTemplateId(Integer templateId) {
        if (templateId == null) {
            return java.util.Collections.emptyList();
        }
        List<MpActivityFormFieldEntity> entities = activityFormFieldService.list(
            new LambdaQueryWrapper<MpActivityFormFieldEntity>()
                .eq(MpActivityFormFieldEntity::getFormId, templateId)
                .orderByAsc(MpActivityFormFieldEntity::getSortOrder)
                .orderByDesc(MpActivityFormFieldEntity::getId)
        );
        
        return entities.stream()
                .map(AdminActivityFormFieldConvert.INSTANCE::convertToResp)
                .collect(Collectors.toList());
    }
}