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

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.seer.teach.common.PageListBean;
import com.seer.teach.common.enums.ResultCodeEnum;
import com.seer.teach.common.exception.CommonException;
import com.seer.teach.common.utils.AssertUtils;
import com.seer.teach.common.utils.PageConverterUtils;
import com.seer.teach.mp.app.controller.req.AppAgentActivityQueryReq;
import com.seer.teach.mp.app.controller.req.MpGenerateQrCodeReq;
import com.seer.teach.mp.app.controller.resp.AppActivityResp;
import com.seer.teach.mp.app.controller.resp.MpQrCodeResp;
import com.seer.teach.mp.app.convert.AppAgentActivityConvert;
import com.seer.teach.mp.app.service.AppOfficialQrCodeService;
import com.seer.teach.mp.app.service.IAppActivityService;
import com.seer.teach.mp.entity.MpActivityEntity;
import com.seer.teach.mp.entity.MpAgentActivityParticipantEntity;
import com.seer.teach.mp.service.IMpActivityService;
import com.seer.teach.mp.service.IMpAgentActivityParticipantService;
import com.seer.teach.mp.service.IMpAgentService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Objects;

/**
 * <p>
 * APP端代理商活动服务实现类
 * </p>
 *
 * @author Lingma
 * @since 2025-12-30
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class AppActivityServiceImpl implements IAppActivityService {

    private final IMpActivityService activityService;

    private final AppOfficialQrCodeService officialQrCodeService;

    private final IMpAgentActivityParticipantService agentActivityParticipantService;

    @Override
    public PageListBean<AppActivityResp> pageList(AppAgentActivityQueryReq query, Integer agentId) {
        log.info("查询参数：{}", query);
        IPage<MpActivityEntity> page = new Page<>(query.getPageNo(), query.getPageSize());
        var pageResult = activityService.page(page, new LambdaQueryWrapper<>(MpActivityEntity.class)
                .like(StringUtils.isNotBlank(query.getActivityName()), MpActivityEntity::getActivityName, query.getActivityName())
                .eq(Objects.nonNull(query.getStatus()), MpActivityEntity::getStatus, query.getStatus()));
        if(Objects.isNull(pageResult) || CollectionUtil.isEmpty(pageResult.getRecords())){
            log.info("查询结果为空");
        }
        log.info("查询代理商活动数量：{}", pageResult.getTotal());
        return PageConverterUtils.convertPageListBean(pageResult, AppAgentActivityConvert.INSTANCE::convertToRespList);
    }

    @Override
    public AppActivityResp getById(Integer id) {
        MpActivityEntity entity = activityService.getById(id);
        return AppAgentActivityConvert.INSTANCE.convertToResp(entity);
    }

    @Override
    public String getQrCode(Integer userId,Integer agentId,Integer activityId,String appId) {
        MpActivityEntity activity = activityService.getById(activityId);
        AssertUtils.notNull(activity, ResultCodeEnum.INVALID_ACTIVITY);

        if(activity.getStatus() != 1){
            throw new CommonException(ResultCodeEnum.INVALID_ACTIVITY);
        }
        MpAgentActivityParticipantEntity relation = agentActivityParticipantService.getParticipantsByActivityAndAgent(activityId, agentId);
        AssertUtils.notNull(relation, ResultCodeEnum.INVALID_ACTIVITY);
        if(StringUtils.isNotBlank(relation.getQrCodeUrl())){
            return relation.getQrCodeUrl();
        }
        MpGenerateQrCodeReq req = new MpGenerateQrCodeReq();
        req.setSceneStr("agentId=" + agentId + "&activityId=" + activityId);
        req.setAppId(appId);
        req.setType(2);
        MpQrCodeResp mpQrCodeResp = officialQrCodeService.generateQrCode(req);
        relation.setQrCodeUrl(mpQrCodeResp.getQrCodeUrl());
        agentActivityParticipantService.updateById(relation);
        return mpQrCodeResp.getQrCodeUrl();
    }
}