package com.example.demo;

import com.example.demo.model.Event;
import com.example.demo.model.enums.EventSourceType;
import com.example.demo.model.enums.EventStatus;
import com.example.demo.model.enums.ThreatLevel;
import com.example.demo.service.EventService;
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.time.LocalDateTime;
import java.util.Optional;

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

/**
 * 事件服务单元测试。
 * <p>
 * 测试事件接收、清洗、目标价值评估等核心业务逻辑。
 * </p>
 *
 * @requirement(name="事件接收与解析", id="SRS-EHTG-01")
 * @requirement(name="数据清洗与标准化", id="SRS-EHTG-02")
 * @requirement(name="高价值目标评估", id="SRS-EHTG-05")
 */
@SpringBootTest
class EventServiceTest {

    @Autowired
    private EventService eventService;

    @Test
    @DisplayName("测试接收事件 - 正常流程")
    void testReceiveEvent() {
        Event event = new Event();
        event.setSourceType(EventSourceType.RADAR);
        event.setOccurTime(LocalDateTime.now());
        event.setLocation("{\"lng\": 116.397, \"lat\": 39.908}");
        event.setConfidence(0.95);
        event.setThreatLevel(ThreatLevel.HIGH);
        event.setEventType("临机");

        Event saved = eventService.receiveEvent(event);

        assertNotNull(saved);
        assertNotNull(saved.getEventId());
        assertEquals(EventStatus.PENDING, saved.getStatus());
        assertNotNull(saved.getCreateTime());
    }

    @Test
    @DisplayName("测试接收事件 - 置信度修正")
    void testReceiveEventWithInvalidConfidence() {
        Event event = new Event();
        event.setSourceType(EventSourceType.OPTOELECTRONIC);
        event.setOccurTime(LocalDateTime.now());
        event.setLocation("{\"lng\": 121.473, \"lat\": 31.230}");
        event.setConfidence(2.5); // 无效置信度
        event.setThreatLevel(ThreatLevel.CRITICAL);
        event.setEventType("临机");

        Event saved = eventService.receiveEvent(event);
        assertEquals(1.0, saved.getConfidence(), 0.001);
    }

    @Test
    @DisplayName("测试目标价值评估 - 高价值目标")
    void testEvaluateHighValueTarget() {
        Event event = new Event();
        event.setEventId("EVT-TEST-001");
        event.setThreatLevel(ThreatLevel.CRITICAL); // 等级4
        event.setConfidence(0.95);

        double score = eventService.evaluateTargetValue(event);
        // 4 * 0.95 * 100 = 380
        assertEquals(380.0, score, 0.01);
        assertTrue(eventService.isHighValueTarget(score));
    }

    @Test
    @DisplayName("测试目标价值评估 - 低价值目标")
    void testEvaluateLowValueTarget() {
        Event event = new Event();
        event.setEventId("EVT-TEST-002");
        event.setThreatLevel(ThreatLevel.LOW); // 等级1
        event.setConfidence(0.5);

        double score = eventService.evaluateTargetValue(event);
        // 1 * 0.5 * 100 = 50
        assertEquals(50.0, score, 0.01);
        assertFalse(eventService.isHighValueTarget(score));
    }

    @Test
    @DisplayName("测试查询事件 - 按ID查询")
    void testGetEventById() {
        Optional<Event> event = eventService.getEventById("EVT-001");
        assertTrue(event.isPresent());
        assertEquals("EVT-001", event.get().getEventId());
    }

    @Test
    @DisplayName("测试查询事件 - 不存在的ID")
    void testGetEventByIdNotFound() {
        Optional<Event> event = eventService.getEventById("NONEXISTENT");
        assertFalse(event.isPresent());
    }

    @Test
    @DisplayName("测试更新事件状态")
    void testUpdateEventStatus() {
        Optional<Event> updated = eventService.updateEventStatus("EVT-001", EventStatus.PROCESSING);
        assertTrue(updated.isPresent());
        assertEquals(EventStatus.PROCESSING, updated.get().getStatus());
    }
}
