package com.erp.tenant.service.nacos;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import com.alibaba.cloud.nacos.NacosConfigManager;
import org.springframework.stereotype.Service;

import java.util.Properties;

/**
 * Nacos配置服务
 * 
 * 用于将租户套餐配置推送到Nacos，实现动态下发。
 */
@Slf4j
@Service
public class NacosConfigService {

    private final NacosConfigManager nacosConfigManager;

    @Value("${nacos.config.namespace:}")
    private String namespace;

    @Value("${spring.application.name:tenant-service}")
    private String applicationName;

    public NacosConfigService(NacosConfigManager nacosConfigManager) {
        this.nacosConfigManager = nacosConfigManager;
    }

    /**
     * 发布租户套餐配置到Nacos
     * 
     * @param tenantId  租户ID
     * @param packageId  套餐ID
     * @param configKey 配置键
     * @param configValue 配置值
     */
    public void publishTenantConfig(Long tenantId, Long packageId, String configKey, String configValue) {
        try {
            String dataId = String.format("tenant-%d-package-%d", tenantId, packageId);
            String group = "TENANT_CONFIG";

            Properties properties = new Properties();
            properties.setProperty(configKey, configValue);

            nacosConfigManager.getConfigService().publishConfig(
                    dataId,
                    group,
                    configValue,
                    "yaml"
            );

            log.info("Nacos配置发布成功: dataId={}, group={}, key={}", dataId, group, configKey);
        } catch (Exception e) {
            log.error("Nacos配置发布失败: tenantId={}, packageId={}, key={}, error={}",
                    tenantId, packageId, configKey, e.getMessage());
        }
    }

    /**
     * 发布租户完整套餐配置（JSON格式）
     * 
     * @param tenantId  租户ID
     * @param packageId  套餐ID
     * @param configJson 套餐完整配置JSON
     */
    public void publishTenantPackageConfig(Long tenantId, Long packageId, String configJson) {
        try {
            String dataId = String.format("tenant-%d-package-%d", tenantId, packageId);
            String group = "TENANT_CONFIG";

            nacosConfigManager.getConfigService().publishConfig(
                    dataId,
                    group,
                    configJson,
                    "yaml"
            );

            log.info("Nacos套餐配置发布成功: tenantId={}, packageId={}, dataId={}", tenantId, packageId, dataId);
        } catch (Exception e) {
            log.error("Nacos套餐配置发布失败: tenantId={}, packageId={}, error={}",
                    tenantId, packageId, e.getMessage());
        }
    }

    /**
     * 删除租户套餐配置
     */
    public void deleteTenantConfig(Long tenantId, Long packageId) {
        try {
            String dataId = String.format("tenant-%d-package-%d", tenantId, packageId);
            String group = "TENANT_CONFIG";

            nacosConfigManager.getConfigService().removeConfig(dataId, group);
            log.info("Nacos配置删除成功: dataId={}", dataId);
        } catch (Exception e) {
            log.error("Nacos配置删除失败: tenantId={}, packageId={}, error={}",
                    tenantId, packageId, e.getMessage());
        }
    }

    /**
     * 发布功能开关配置到Nacos
     * 
     * @param tenantId   租户ID
     * @param featureKey 功能开关键
     * @param enabled    是否启用
     */
    public void publishFeatureFlag(Long tenantId, String featureKey, boolean enabled) {
        try {
            String dataId = String.format("tenant-%d-features", tenantId);
            String group = "TENANT_FEATURE";

            String content = String.format("%s: %s", featureKey, enabled);

            nacosConfigManager.getConfigService().publishConfig(
                    dataId,
                    group,
                    content,
                    "yaml"
            );

            log.info("Nacos功能开关发布成功: tenantId={}, feature={}, enabled={}", tenantId, featureKey, enabled);
        } catch (Exception e) {
            log.error("Nacos功能开关发布失败: tenantId={}, feature={}, error={}",
                    tenantId, featureKey, e.getMessage());
        }
    }
}
