package com.ycwl.basic.product.service;

import com.ycwl.basic.product.capability.DuplicateCheckStrategy;
import com.ycwl.basic.product.capability.PricingMode;
import com.ycwl.basic.product.capability.ProductTypeCapability;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import static org.junit.jupiter.api.Assertions.*;

/**
 * 商品类型能力服务测试
 */
@SpringBootTest
public class ProductTypeCapabilityServiceTest {

    @Autowired(required = false)
    private IProductTypeCapabilityService productTypeCapabilityService;

    @Test
    public void testGetCapability_VLOG_VIDEO() {
        if (productTypeCapabilityService == null) {
            System.out.println("服务未注入，跳过测试（可能数据库未迁移）");
            return;
        }

        ProductTypeCapability capability = productTypeCapabilityService.getCapability("VLOG_VIDEO");

        assertNotNull(capability);
        assertEquals("VLOG_VIDEO", capability.getProductType());
        assertEquals("Vlog视频", capability.getDisplayName());
        assertEquals("VLOG", capability.getCategory());
        assertEquals(PricingMode.FIXED, capability.getPricingModeEnum());
        assertEquals(false, capability.getAllowDuplicatePurchase());
        assertEquals(DuplicateCheckStrategy.UNIQUE_RESOURCE, capability.getDuplicateCheckStrategyEnum());
    }

    @Test
    public void testGetCapability_PHOTO_PRINT() {
        if (productTypeCapabilityService == null) {
            System.out.println("服务未注入，跳过测试（可能数据库未迁移）");
            return;
        }

        ProductTypeCapability capability = productTypeCapabilityService.getCapability("PHOTO_PRINT");

        assertNotNull(capability);
        assertEquals("PHOTO_PRINT", capability.getProductType());
        assertEquals("照片打印", capability.getDisplayName());
        assertEquals("PRINT", capability.getCategory());
        assertEquals(PricingMode.QUANTITY_BASED, capability.getPricingModeEnum());
        assertEquals(true, capability.getAllowDuplicatePurchase());
        assertEquals(DuplicateCheckStrategy.NO_CHECK, capability.getDuplicateCheckStrategyEnum());
    }

    @Test
    public void testGetDisplayName() {
        if (productTypeCapabilityService == null) {
            System.out.println("服务未注入，跳过测试（可能数据库未迁移）");
            return;
        }

        String displayName = productTypeCapabilityService.getDisplayName("RECORDING_SET");
        assertEquals("录像集", displayName);
    }

    @Test
    public void testAllowDuplicatePurchase() {
        if (productTypeCapabilityService == null) {
            System.out.println("服务未注入，跳过测试（可能数据库未迁移）");
            return;
        }

        // 视频类不允许重复购买
        assertFalse(productTypeCapabilityService.allowDuplicatePurchase("VLOG_VIDEO"));

        // 打印类允许重复购买
        assertTrue(productTypeCapabilityService.allowDuplicatePurchase("PHOTO_PRINT"));
    }

    @Test
    public void testGetPricingMode() {
        if (productTypeCapabilityService == null) {
            System.out.println("服务未注入，跳过测试（可能数据库未迁移）");
            return;
        }

        // 视频类固定价格
        assertEquals(PricingMode.FIXED, productTypeCapabilityService.getPricingMode("VLOG_VIDEO"));

        // 打印类基于数量
        assertEquals(PricingMode.QUANTITY_BASED, productTypeCapabilityService.getPricingMode("PHOTO_PRINT"));
    }

    @Test
    public void testGetCapability_NotFound_ShouldReturnDefault() {
        if (productTypeCapabilityService == null) {
            System.out.println("服务未注入，跳过测试（可能数据库未迁移）");
            return;
        }

        ProductTypeCapability capability = productTypeCapabilityService.getCapability("NOT_EXIST_TYPE");

        // 应该返回默认配置而不是 null
        assertNotNull(capability);
        assertEquals("NOT_EXIST_TYPE", capability.getProductType());
        assertEquals("景区商品", capability.getDisplayName());
        assertEquals(PricingMode.FIXED, capability.getPricingModeEnum());
    }

    @Test
    public void testGetCapability_NullOrEmpty_ShouldReturnDefault() {
        if (productTypeCapabilityService == null) {
            System.out.println("服务未注入，跳过测试（可能数据库未迁移）");
            return;
        }

        ProductTypeCapability capability1 = productTypeCapabilityService.getCapability(null);
        assertNotNull(capability1);
        assertEquals("景区商品", capability1.getDisplayName());

        ProductTypeCapability capability2 = productTypeCapabilityService.getCapability("");
        assertNotNull(capability2);
        assertEquals("景区商品", capability2.getDisplayName());
    }
}
