package com.example.demo;

import com.example.demo.model.TaskScheme;
import com.example.demo.model.dto.SchemeCompareDTO;
import com.example.demo.service.SchemeService;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Optional;

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

/**
 * 任务方案管理服务单元测试。
 * <p>
 * 测试方案列表、详情、对比、重构、分发等核心业务逻辑。
 * </p>
 *
 * @requirement(name="集中式/分布式方案列表展示", id="SRS-TMS-01")
 * @requirement(name="方案详情查看", id="SRS-TMS-03")
 * @requirement(name="多方案对比分析", id="SRS-TMS-04")
 * @requirement(name="方案重构操作", id="SRS-TMS-06")
 * @requirement(name="方案分发驱动", id="SRS-TMS-08")
 */
@SpringBootTest
class SchemeServiceTest {

    @Autowired
    private SchemeService schemeService;

    @Test
    @DisplayName("测试查询方案列表")
    void testListSchemes() {
        List<TaskScheme> schemes = schemeService.listSchemes("createTime", "desc", 0, 10);
        assertNotNull(schemes);
        assertFalse(schemes.isEmpty());
    }

    @Test
    @DisplayName("测试按优先级排序查询方案列表")
    void testListSchemesSortedByPriority() {
        List<TaskScheme> schemes = schemeService.listSchemes("priority", "desc", 0, 10);
        assertNotNull(schemes);
        if (schemes.size() >= 2) {
            assertTrue(schemes.get(0).getPriority() >= schemes.get(1).getPriority());
        }
    }

    @Test
    @DisplayName("测试查询方案详情")
    void testGetSchemeDetail() {
        Optional<TaskScheme> scheme = schemeService.getSchemeDetail("SCH-001");
        assertTrue(scheme.isPresent());
        assertEquals("SCH-001", scheme.get().getSchemeId());
        assertNotNull(scheme.get().getDeploymentDetail());
        assertNotNull(scheme.get().getRouteDetail());
        assertNotNull(scheme.get().getAlgorithmTrace());
    }

    @Test
    @DisplayName("测试查询不存在的方案详情")
    void testGetSchemeDetailNotFound() {
        Optional<TaskScheme> scheme = schemeService.getSchemeDetail("NONEXISTENT");
        assertFalse(scheme.isPresent());
    }

    @Test
    @DisplayName("测试多方案对比分析")
    void testCompareSchemes() {
        SchemeCompareDTO dto = new SchemeCompareDTO();
        dto.setSchemeIds(Arrays.asList("SCH-001", "SCH-002", "SCH-003"));
        dto.setCompareFields(Arrays.asList("estimatedDuration", "estimatedLossRatio", "successRate", "priority"));

        Map<String, Object> result = schemeService.compareSchemes(dto);
        assertNotNull(result);
        assertTrue(result.containsKey("schemes"));
    }

    @Test
    @DisplayName("测试方案重构")
    void testReconstructScheme() {
        TaskScheme newScheme = schemeService.reconstructScheme("SCH-001", "{\"maxDuration\": 90}");
        assertNotNull(newScheme);
        assertNotNull(newScheme.getSchemeId());
        assertEquals("SCH-001", newScheme.getParentSchemeId());
        assertEquals(2, newScheme.getVersion()); // 原版本1，重构后版本2
    }

    @Test
    @DisplayName("测试方案分发")
    void testDispatchScheme() {
        Map<String, Object> result = schemeService.dispatchScheme("SCH-002", "NODE-A1");
        assertNotNull(result);
        assertEquals("SCH-002", result.get("schemeId"));
        assertNotNull(result.get("instructions"));
    }
}
