package com.gxwebsoft.common.system.service;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.gxwebsoft.cms.entity.CmsWebsiteField;
import com.gxwebsoft.cms.service.CmsWebsiteFieldService;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.ActiveProfiles;

import javax.annotation.Resource;
import java.util.List;

/**
 * 微信小程序配置测试
 *
 * @author WebSoft
 * @since 2025-08-23
 */
@Slf4j
@SpringBootTest
@ActiveProfiles("dev")
public class WeixinConfigTest {

    @Resource
    private SettingService settingService;
    
    @Resource
    private CmsWebsiteFieldService cmsWebsiteFieldService;

    /**
     * 测试从cms_website_field表获取微信小程序配置
     */
    @Test
    public void testGetWeixinConfigFromWebsiteField() {
        Integer tenantId = 10550;
        
        log.info("=== 开始测试从cms_website_field表获取微信小程序配置 ===");

        // 1. 查看cms_website_field表中的所有配置
        List<CmsWebsiteField> allFields = cmsWebsiteFieldService.list(
            new LambdaQueryWrapper<CmsWebsiteField>()
                .eq(CmsWebsiteField::getTenantId, tenantId)
                .eq(CmsWebsiteField::getDeleted, 0)
        );
        
        log.info("租户{}的所有cms_website_field配置:", tenantId);
        for (CmsWebsiteField field : allFields) {
            log.info("  - ID: {}, Name: {}, Value: {}", field.getId(), field.getName(), field.getValue());
        }

        // 2. 查找AppID配置
        CmsWebsiteField appIdField = cmsWebsiteFieldService.getOne(
            new LambdaQueryWrapper<CmsWebsiteField>()
                .eq(CmsWebsiteField::getName, "AppID")
                .eq(CmsWebsiteField::getTenantId, tenantId)
                .eq(CmsWebsiteField::getDeleted, 0)
        );
        
        log.info("AppID配置: {}", appIdField);

        // 3. 查找AppSecret配置
        CmsWebsiteField appSecretField = cmsWebsiteFieldService.getOne(
            new LambdaQueryWrapper<CmsWebsiteField>()
                .eq(CmsWebsiteField::getName, "AppSecret")
                .eq(CmsWebsiteField::getTenantId, tenantId)
                .eq(CmsWebsiteField::getDeleted, 0)
        );
        
        log.info("AppSecret配置: {}", appSecretField);

        // 4. 测试获取微信小程序配置
        try {
            JSONObject config = settingService.getBySettingKeyIgnoreTenant("mp-weixin", tenantId);
            log.info("✅ 成功获取微信小程序配置: {}", config);
        } catch (Exception e) {
            log.error("❌ 获取微信小程序配置失败: {}", e.getMessage());
        }

        log.info("=== 微信小程序配置测试完成 ===");
    }

    /**
     * 测试不同name的查询
     */
    @Test
    public void testDifferentNameQueries() {
        Integer tenantId = 10550;
        
        log.info("=== 开始测试不同name的查询 ===");

        String[] nameVariations = {"AppID", "appId", "APPID", "app_id", "AppSecret", "appSecret", "APPSECRET", "app_secret"};
        
        for (String name : nameVariations) {
            CmsWebsiteField field = cmsWebsiteFieldService.getOne(
                new LambdaQueryWrapper<CmsWebsiteField>()
                    .eq(CmsWebsiteField::getName, name)
                    .eq(CmsWebsiteField::getTenantId, tenantId)
                    .eq(CmsWebsiteField::getDeleted, 0)
            );
            
            if (field != null) {
                log.info("找到配置 - Name: {}, Value: {}", name, field.getValue());
            } else {
                log.info("未找到配置 - Name: {}", name);
            }
        }

        log.info("=== 不同name查询测试完成 ===");
    }

    /**
     * 测试创建测试配置
     */
    @Test
    public void testCreateTestConfig() {
        Integer tenantId = 10550;
        
        log.info("=== 开始创建测试配置 ===");

        // 创建AppID配置
        CmsWebsiteField appIdField = new CmsWebsiteField();
        appIdField.setName("AppID");
        appIdField.setValue("wx1234567890abcdef"); // 测试AppID
        appIdField.setTenantId(tenantId);
        appIdField.setType(0); // 文本类型
        appIdField.setComments("微信小程序AppID");
        appIdField.setDeleted(0);
        
        // 创建AppSecret配置
        CmsWebsiteField appSecretField = new CmsWebsiteField();
        appSecretField.setName("AppSecret");
        appSecretField.setValue("abcdef1234567890abcdef1234567890"); // 测试AppSecret
        appSecretField.setTenantId(tenantId);
        appSecretField.setType(0); // 文本类型
        appSecretField.setComments("微信小程序AppSecret");
        appSecretField.setDeleted(0);

        try {
            // 检查是否已存在
            CmsWebsiteField existingAppId = cmsWebsiteFieldService.getOne(
                new LambdaQueryWrapper<CmsWebsiteField>()
                    .eq(CmsWebsiteField::getName, "AppID")
                    .eq(CmsWebsiteField::getTenantId, tenantId)
            );
            
            if (existingAppId == null) {
                cmsWebsiteFieldService.save(appIdField);
                log.info("✅ 创建AppID配置成功");
            } else {
                log.info("AppID配置已存在，跳过创建");
            }

            CmsWebsiteField existingAppSecret = cmsWebsiteFieldService.getOne(
                new LambdaQueryWrapper<CmsWebsiteField>()
                    .eq(CmsWebsiteField::getName, "AppSecret")
                    .eq(CmsWebsiteField::getTenantId, tenantId)
            );
            
            if (existingAppSecret == null) {
                cmsWebsiteFieldService.save(appSecretField);
                log.info("✅ 创建AppSecret配置成功");
            } else {
                log.info("AppSecret配置已存在，跳过创建");
            }

        } catch (Exception e) {
            log.error("❌ 创建测试配置失败: {}", e.getMessage());
        }

        log.info("=== 创建测试配置完成 ===");
    }
}
