package shop.alien.store.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import shop.alien.entity.storePlatform.StoreOperationalActivityAchievement;
import shop.alien.entity.storePlatform.vo.StoreOperationalActivityAchievementVo;
import shop.alien.mapper.storePlantform.StoreOperationalActivityAchievementMapper;
import shop.alien.store.dto.StoreOperationalActivityAchievementDto;
import shop.alien.store.service.StoreOperationalActivityAchievementService;

/**
 * 运营活动成果服务实现
 *
 * @author system
 * @since 2025-01-XX
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class StoreOperationalActivityAchievementServiceImpl implements StoreOperationalActivityAchievementService {

    private final StoreOperationalActivityAchievementMapper achievementMapper;

    @Override
    public boolean addAchievement(StoreOperationalActivityAchievementDto dto) {
        if (dto == null) {
            throw new IllegalArgumentException("参数不能为空");
        }
        if (dto.getActivityId() == null) {
            throw new IllegalArgumentException("活动ID不能为空");
        }
        if (dto.getUserId() == null) {
            throw new IllegalArgumentException("用户ID不能为空");
        }
        if (dto.getSignupId() == null) {
            throw new IllegalArgumentException("报名ID不能为空");
        }

        StoreOperationalActivityAchievement achievement = new StoreOperationalActivityAchievement();
        achievement.setActivityId(dto.getActivityId());
        achievement.setUserId(dto.getUserId());
        achievement.setSignupId(dto.getSignupId());
        achievement.setAchievementDesc(dto.getAchievementDesc());
        achievement.setMediaUrls(dto.getMediaUrls());
        achievement.setDeleteFlag(0);
        achievement.setCreatedUserId(dto.getUserId());
        return achievementMapper.insert(achievement) > 0;
    }

    @Override
    public StoreOperationalActivityAchievementVo getAchievementDetail(Integer id) {
        if (id == null) {
            throw new IllegalArgumentException("成果ID不能为空");
        }
        StoreOperationalActivityAchievement achievement = achievementMapper.selectById(id);
        if (achievement == null || achievement.getDeleteFlag() != null && achievement.getDeleteFlag() == 1) {
            return null;
        }
        StoreOperationalActivityAchievementVo vo = new StoreOperationalActivityAchievementVo();
        BeanUtils.copyProperties(achievement, vo);
        vo.setMediaUrlList(splitMediaUrls(achievement.getMediaUrls()));
        return vo;
    }

    @Override
    public IPage<StoreOperationalActivityAchievementVo> listAchievements(Integer activityId, Integer userId, Integer signupId,
                                                                         Integer pageNum, Integer pageSize) {
        int current = pageNum == null || pageNum <= 0 ? 1 : pageNum;
        int size = pageSize == null || pageSize <= 0 ? 10 : pageSize;
        Page<StoreOperationalActivityAchievement> page = new Page<>(current, size);

        LambdaQueryWrapper<StoreOperationalActivityAchievement> wrapper = new LambdaQueryWrapper<>();
        if (activityId != null) {
            wrapper.eq(StoreOperationalActivityAchievement::getActivityId, activityId);
        }
        if (userId != null) {
            wrapper.eq(StoreOperationalActivityAchievement::getUserId, userId);
        }
        if (signupId != null) {
            wrapper.eq(StoreOperationalActivityAchievement::getSignupId, signupId);
        }
        wrapper.eq(StoreOperationalActivityAchievement::getDeleteFlag, 0)
                .orderByDesc(StoreOperationalActivityAchievement::getCreatedTime);

        IPage<StoreOperationalActivityAchievement> entityPage = achievementMapper.selectPage(page, wrapper);
        Page<StoreOperationalActivityAchievementVo> voPage = new Page<>(entityPage.getCurrent(), entityPage.getSize(), entityPage.getTotal());
        if (entityPage.getRecords() != null) {
            List<StoreOperationalActivityAchievementVo> voList = new ArrayList<>();
            for (StoreOperationalActivityAchievement item : entityPage.getRecords()) {
                if (item == null) {
                    continue;
                }
                StoreOperationalActivityAchievementVo vo = new StoreOperationalActivityAchievementVo();
                BeanUtils.copyProperties(item, vo);
                vo.setMediaUrlList(splitMediaUrls(item.getMediaUrls()));
                voList.add(vo);
            }
            voPage.setRecords(voList);
        }
        return voPage;
    }

    private List<String> splitMediaUrls(String mediaUrls) {
        if (mediaUrls == null || mediaUrls.trim().isEmpty()) {
            return Collections.emptyList();
        }
        String[] parts = mediaUrls.split(",");
        List<String> results = new ArrayList<>();
        for (String part : parts) {
            if (part != null && !part.trim().isEmpty()) {
                results.add(part.trim());
            }
        }
        return results;
    }
}
