package com.jeesharp.business.modules.coupon.application.support;

import cn.hutool.core.util.StrUtil;
import com.jeesharp.business.modules.common.api.BizSceneApi;
import com.jeesharp.business.modules.common.bizscene.enums.BizSceneCapabilityEnum;
import com.jeesharp.business.modules.common.bizscene.support.BizSceneCouponLegacySupport;
import com.jeesharp.business.modules.common.bizscene.vo.BizSceneOptionVO;
import com.jeesharp.business.modules.coupon.vo.CouponSceneOptionVO;
import com.jeesharp.framework.exception.BizErrorException;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * 优惠券场景与 {@code cfg_biz_scene} 注册表桥接：校验、下拉选项（legacy scene_code 兼容）。
 *
 * @author jeesharp
 * @since 2026-07-03
 */
@Component
@AllArgsConstructor
public class CouponSceneRegistryBridgeSupport {

    private final BizSceneApi bizSceneApi;

    public void assertSceneEnabled(String sceneCode, String tenantId) {
        if (StrUtil.isBlank(sceneCode)) {
            throw new BizErrorException("请选择使用场景");
        }
        String canonical = BizSceneCouponLegacySupport.resolveCanonicalSceneCode(sceneCode);
        BizSceneOptionVO registry = bizSceneApi.getOptionByCode(canonical, tenantId);
        if (registry == null || !containsCapability(registry.getCapabilities(), BizSceneCapabilityEnum.COUPON)) {
            throw new BizErrorException("使用场景无效或未在业态注册表中配置：" + sceneCode);
        }
        bizSceneApi.assertSceneEnabled(canonical, tenantId);
    }

    public List<CouponSceneOptionVO> listEnabledOptions(String tenantId) {
        List<CouponSceneOptionVO> result = new ArrayList<>();
        Set<String> legacyCodes = new HashSet<>();
        for (BizSceneOptionVO registry : bizSceneApi.listEnabledOptions(BizSceneCapabilityEnum.COUPON, tenantId)) {
            CouponSceneOptionVO option = toCouponOption(registry);
            if (option != null && legacyCodes.add(option.getSceneCode())) {
                result.add(option);
            }
        }
        return result;
    }

    public CouponSceneOptionVO getOptionByCode(String sceneCode, String tenantId) {
        if (StrUtil.isBlank(sceneCode)) {
            return null;
        }
        String canonical = BizSceneCouponLegacySupport.resolveCanonicalSceneCode(sceneCode);
        BizSceneOptionVO registry = bizSceneApi.getOptionByCode(canonical, tenantId);
        if (registry != null && containsCapability(registry.getCapabilities(), BizSceneCapabilityEnum.COUPON)) {
            return toCouponOption(registry);
        }
        return null;
    }

    private static CouponSceneOptionVO toCouponOption(BizSceneOptionVO registry) {
        BizSceneCouponLegacySupport.CouponScenePickerFields fields =
                BizSceneCouponLegacySupport.toCouponPickerFields(registry);
        CouponSceneOptionVO vo = new CouponSceneOptionVO();
        vo.setSceneCode(fields.getSceneCode());
        vo.setSceneName(fields.getSceneName());
        vo.setRequestUrl(fields.getRequestUrl());
        vo.setKeywordField(fields.getKeywordField());
        vo.setNameField(fields.getNameField());
        vo.setSupportCategoryBrand(fields.getSupportCategoryBrand());
        vo.setScopeItemLabel(fields.getScopeItemLabel());
        return vo;
    }

    private static boolean containsCapability(String capabilities, String capability) {
        if (StrUtil.isBlank(capabilities) || StrUtil.isBlank(capability)) {
            return false;
        }
        for (String item : capabilities.split(",")) {
            if (capability.equalsIgnoreCase(item.trim())) {
                return true;
            }
        }
        return false;
    }
}
