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

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.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.admin.controller.req.AgentGenerateQrCodeReq;
import com.seer.teach.mp.admin.controller.req.MpActivityQueryReq;
import com.seer.teach.mp.admin.controller.req.MpActivityReq;
import com.seer.teach.mp.admin.controller.resp.AdminActivityResp;
import com.seer.teach.mp.admin.controller.resp.AdminAgentQrCodeResp;
import com.seer.teach.mp.admin.convert.AdminActivityConvert;
import com.seer.teach.mp.admin.service.AdminOfficialQrCodeService;
import com.seer.teach.mp.admin.service.IAdminActivityService;
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 lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Objects;

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

    private final IMpActivityService agentActivityService;

    private final IMpAgentActivityParticipantService agentActivityParticipantService;

    private final AdminOfficialQrCodeService officialQrCodeService;

    @Override
    public PageListBean<AdminActivityResp> pageList(MpActivityQueryReq query) {
        Page<MpActivityEntity> page = new Page<>(query.getPageNo(), query.getPageSize());
        var pageResult = agentActivityService.page(page, new LambdaQueryWrapper<>(MpActivityEntity.class)
                .like(StringUtils.isNotBlank(query.getActivityName()), MpActivityEntity::getActivityName, query.getActivityName())
                .eq(Objects.nonNull(query.getStatus()), MpActivityEntity::getStatus, query.getStatus()));
        return PageConverterUtils.convertPageListBean(pageResult, AdminActivityConvert.INSTANCE::convertToRespList);
    }

    @Override
    public boolean saveOrUpdateActivity(MpActivityReq request) {
        MpActivityEntity entity = AdminActivityConvert.INSTANCE.convert(request);
        return agentActivityService.saveOrUpdateActivity(entity);
    }

    @Override
    public boolean deleteActivity(Integer id) {
        return agentActivityService.deleteActivity(id);
    }

    @Override
    public AdminActivityResp getById(Integer id) {
        MpActivityEntity entity = agentActivityService.getById(id);
        return AdminActivityConvert.INSTANCE.convertToResp(entity);
    }

    @Override
    public String getQrCode(Integer agentId, Integer activityId, String appId) {
        MpActivityEntity activity = agentActivityService.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();
        }
        AgentGenerateQrCodeReq req = new AgentGenerateQrCodeReq();
        req.setSceneStr("agentId=" + agentId + "&activityId=" + activityId);
        req.setAppId(appId);
        req.setType(2);
        AdminAgentQrCodeResp QrCodeResp = officialQrCodeService.generateQrCode(req);
        relation.setQrCodeUrl(QrCodeResp.getQrCodeUrl());
        agentActivityParticipantService.updateById(relation);
        return QrCodeResp.getQrCodeUrl();
    }
}