package com.liquidnet.service.adam.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.liquidnet.service.adam.dto.vo.AdamRealInfoVo;
import com.liquidnet.service.adam.entity.AdamCaomeiBadge;
import com.liquidnet.service.adam.entity.AdamCaomeiPassport;
import com.liquidnet.service.adam.mapper.AdamCaomeiBadgeMapper;
import com.liquidnet.service.adam.mapper.AdamCaomeiPassportMapper;
import com.liquidnet.service.adam.service.AdamRdmService;
import com.liquidnet.service.adam.service.IAdamCaomeiBadgeUserService;
import com.liquidnet.service.base.ErrorMapping;
import com.liquidnet.service.base.ResponseDto;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Slf4j
@Service
public class AdamCaomeiBadgeUserServiceImpl implements IAdamCaomeiBadgeUserService {

    @Autowired
    private AdamCaomeiBadgeMapper adamCaomeiBadgeMapper;
    @Autowired
    private AdamCaomeiPassportMapper adamCaomeiPassportMapper;
    @Autowired
    private AdamRdmService adamRdmService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResponseDto<String> claimBadge(String badgeId, String uid) {
        if (StringUtils.isBlank(uid)) {
            return ResponseDto.failure(ErrorMapping.get("10001"));
        }

        // 1. 查询徽章配置
        AdamCaomeiBadge badge = adamCaomeiBadgeMapper.selectOne(
                Wrappers.lambdaQuery(AdamCaomeiBadge.class)
                        .eq(AdamCaomeiBadge::getBadgeId, badgeId)
                        .eq(AdamCaomeiBadge::getDisplayStatus, 1)
        );
        if (badge == null) {
            log.error("[claimBadge] 徽章不存在或未上架, uid: {}, badgeId: {}", uid, badgeId);
            return ResponseDto.failure(ErrorMapping.get("10607"));
        }

        // 2. 检查用户是否已经领取过该徽章
        int count = adamCaomeiBadgeMapper.checkUserBadgeExists(uid, badgeId);
        if (count > 0) {
            log.info("[claimBadge] 用户已领取过该徽章, uid: {}, badgeId: {}", uid, badgeId);
            return ResponseDto.failure(ErrorMapping.get("10608"));
        }

        int type = badge.getType() == null ? 0 : badge.getType();

        // 3. 根据徽章类型执行不同的认领逻辑
        if (type == 1) {
            // 护照纪念徽章：需要绑定了护照才能领取
            AdamCaomeiPassport bound = adamCaomeiPassportMapper.selectOne(
                    Wrappers.lambdaQuery(AdamCaomeiPassport.class)
                            .eq(AdamCaomeiPassport::getUserId, uid)
                            .eq(AdamCaomeiPassport::getStatus, 1)
                            .last("limit 1")
            );
            if (bound == null) {
                log.error("[claimBadge] 认领护照纪念徽章需先绑定护照, uid: {}, badgeId: {}", uid, badgeId);
                return ResponseDto.failure(ErrorMapping.get("10609"));
            }
            
            // 发放徽章 (source: 1-绑定护照自动发放/护照徽章认领)
            adamCaomeiBadgeMapper.insertUserBadge(uid, badgeId, 1);
            adamRdmService.delUserCaomeiBadgesByUid(uid);
            log.info("[claimBadge] 护照纪念徽章认领成功, uid: {}, badgeId: {}", uid, badgeId);
            return ResponseDto.success(badgeId);

        } else if (type == 2) {
            // 演出纪念徽章：需要实名认证 + 购票记录
            AdamRealInfoVo real = adamRdmService.getRealInfoVoByUidPlain(uid);
            if (real == null || real.getState() == null || real.getState() != 1 || StringUtils.isBlank(real.getIdCard())) {
                log.error("[claimBadge] 认领演出徽章需先实名, uid: {}, badgeId: {}", uid, badgeId);
                return ResponseDto.failure(ErrorMapping.get("10610"));
            }

            String idCard = real.getIdCard();
            List<String> paidPerformanceIds = adamCaomeiBadgeMapper.selectPaidPerformanceIdsByIdCard(idCard);
            if (paidPerformanceIds == null || !paidPerformanceIds.contains(badge.getPerformanceId())) {
                log.error("[claimBadge] 未查询到该演出的购票记录，无法认领, uid: {}, badgeId: {}", uid, badgeId);
                return ResponseDto.failure(ErrorMapping.get("10611"));
            }

            // 自动发放该演出所有关联徽章
//            List<AdamCaomeiBadge> performanceBadges = adamCaomeiBadgeMapper.selectList(
//                    Wrappers.lambdaQuery(AdamCaomeiBadge.class)
//                            .eq(AdamCaomeiBadge::getPerformanceId, badge.getPerformanceId())
//                            .eq(AdamCaomeiBadge::getDisplayStatus, 1)
//                            .eq(AdamCaomeiBadge::getType, 2)
//            );
            
//            for (AdamCaomeiBadge perfBadge : performanceBadges) {
                // 发放徽章 (source: 2-购票自动发放/演出徽章认领)
            adamCaomeiBadgeMapper.insertUserBadge(uid, badgeId, 2);
            adamRdmService.delUserCaomeiBadgesByUid(uid);
//            }
            
            log.info("[claimBadge] 演出纪念徽章认领成功, uid: {}, badgeId: {}, 发放数量: {}", uid, badgeId, 1);
            return ResponseDto.success(badgeId);

        } else if (type == 3) {
            // 特殊徽章不可自助领取
            log.error("[claimBadge] 特殊徽章不可自助领取, uid: {}, badgeId: {}", uid, badgeId);
            return ResponseDto.failure(ErrorMapping.get("10612"));
        }

        log.error("[claimBadge] 未知的徽章类型, uid: {}, badgeId: {}, type: {}", uid, badgeId, type);
        return ResponseDto.failure(ErrorMapping.get("10613"));
    }
}
