package com.jeesharp.business.modules.marketing.tenant.application;

import cn.hutool.core.util.StrUtil;
import com.jeesharp.business.modules.marketing.tenant.mapper.MarketingTenantReadMapper;
import com.jeesharp.business.modules.marketing.tenant.vo.MarketingTenantBriefVO;
import com.jeesharp.business.modules.marketing.tenant.vo.MarketingTenantLandingVO;
import com.jeesharp.business.modules.marketing.tenantExt.application.MarketingTenantExtApplicationService;
import com.jeesharp.business.modules.marketing.tenantExt.vo.MarketingTenantExtInfoVO;
import com.jeesharp.business.modules.marketing.tenantQrcode.application.MarketingTenantQrcodeApplicationService;
import com.jeesharp.business.modules.marketing.tenantQrcode.entity.MarketingTenantQrcode;
import com.jeesharp.framework.exception.BizErrorException;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.Collections;

/**
 * 扫码落地页应用服务（免登录）。
 *
 * @author jeesharp
 * @since 2026-06-25
 */
@Service
@AllArgsConstructor
@Transactional(readOnly = true)
public class MarketingTenantLandingApplicationService {

    private static final int STATUS_ENABLED = 1;

    private final MarketingTenantQrcodeApplicationService marketingTenantQrcodeApplicationService;
    private final MarketingTenantExtApplicationService marketingTenantExtApplicationService;
    private final MarketingTenantReadMapper marketingTenantReadMapper;

    @Transactional(readOnly = false)
    public MarketingTenantLandingVO resolveLandingByScene(String scene) {
        if (StrUtil.isBlank(scene)) {
            throw new BizErrorException("scene 不能为空");
        }
        MarketingTenantQrcode qrcode = marketingTenantQrcodeApplicationService.getByWxScene(scene.trim());
        if (qrcode == null) {
            throw new BizErrorException("推广码无效");
        }
        if (qrcode.getStatus() == null || qrcode.getStatus() != STATUS_ENABLED) {
            throw new BizErrorException("推广码已停用");
        }
        if (qrcode.getExpireDate() != null && qrcode.getExpireDate().isBefore(LocalDateTime.now())) {
            throw new BizErrorException("推广码已过期");
        }
        marketingTenantQrcodeApplicationService.incrementScanCount(qrcode.getId());

        MarketingTenantBriefVO tenant = loadTenant(qrcode.getTenantId());
        MarketingTenantExtInfoVO ext = marketingTenantExtApplicationService.getByTenantId(qrcode.getTenantId());

        MarketingTenantLandingVO vo = new MarketingTenantLandingVO();
        vo.setTenantId(tenant.getId());
        vo.setTenantCode(tenant.getTenantCode());
        vo.setTenantName(tenant.getTenantName());
        vo.setMarketingEnabled(ext.getMarketingEnabled());
        vo.setAutoJoinOnQr(ext.getAutoJoinOnQr());
        vo.setWelcomeTitle(ext.getWelcomeTitle());
        vo.setWelcomeSubtitle(ext.getWelcomeSubtitle());
        vo.setLogoUrl(ext.getLogoUrl());
        vo.setChannelCode(qrcode.getChannelCode());
        vo.setPagePath(qrcode.getPagePath());
        return vo;
    }

    private MarketingTenantBriefVO loadTenant(String tenantId) {
        var list = marketingTenantReadMapper.listByIds(Collections.singletonList(tenantId));
        if (list == null || list.isEmpty()) {
            throw new BizErrorException("店铺不存在或已停用");
        }
        MarketingTenantBriefVO tenant = list.get(0);
        if (tenant.getStatus() != null && tenant.getStatus() == 0) {
            throw new BizErrorException("店铺已停用");
        }
        return tenant;
    }
}
