package com.qaup.collision.datacollector.filter;

import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.ArgumentMatchers.*;
import static org.mockito.Mockito.*;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;

import com.qaup.collision.common.model.AirportVehicle;
import com.qaup.system.service.ISysConfigService;
import com.qaup.system.service.ISysVehicleInfoService;

/**
 * VehicleLocationFilter 单元测试
 * 
 * @author AI Assistant
 * @version 1.0
 * @since 2025-01-31
 */
@ExtendWith(MockitoExtension.class)
class VehicleLocationFilterTest {

    @Mock
    private ISysConfigService configService;

    @Mock
    private ISysVehicleInfoService vehicleInfoService;

    @InjectMocks
    private VehicleLocationFilter vehicleLocationFilter;

    private List<AirportVehicle> testVehicles;

    @BeforeEach
    void setUp() {
        // 准备测试数据
        AirportVehicle vehicle1 = AirportVehicle.builder()
            .objectId("京A12345")
            .objectName("京A12345")
            .build();

        AirportVehicle vehicle2 = AirportVehicle.builder()
            .objectId("京B67890")
            .objectName("京B67890")
            .build();

        AirportVehicle vehicle3 = AirportVehicle.builder()
            .objectId("京C11111")
            .objectName("京C11111")
            .build();

        // 没有车牌号的车辆
        AirportVehicle vehicleNoPlate = AirportVehicle.builder()
            .objectId("")
            .objectName("")
            .build();

        testVehicles = Arrays.asList(vehicle1, vehicle2, vehicle3, vehicleNoPlate);
    }

    @Test
    void testFilterVehicles_FilterDisabled_ShouldReturnAllVehicles() {
        // 配置过滤功能禁用
        when(configService.selectConfigByKey("unmanaged.vehicle.filter.enabled"))
            .thenReturn("false");

        // 执行过滤
        List<AirportVehicle> result = vehicleLocationFilter.filterVehicles(testVehicles);

        // 验证结果
        assertEquals(4, result.size());
        assertEquals(testVehicles, result);
        
        // 验证没有调用车辆信息查询
        verify(vehicleInfoService, never()).selectExistingLicensePlates(any());
    }

    @Test
    void testFilterVehicles_FilterEnabled_ShouldFilterUnregisteredVehicles() {
        // 配置过滤功能启用
        when(configService.selectConfigByKey("unmanaged.vehicle.filter.enabled"))
            .thenReturn("true");

        // 模拟只有京A12345和京C11111在数据库中存在
        when(vehicleInfoService.selectExistingLicensePlates(anyList()))
            .thenReturn(Arrays.asList("京A12345", "京C11111"));

        // 执行过滤
        List<AirportVehicle> result = vehicleLocationFilter.filterVehicles(testVehicles);

        // 验证结果：应该只返回2辆注册的车辆
        assertEquals(2, result.size());
        assertTrue(result.stream().anyMatch(v -> "京A12345".equals(v.getPlateNumber())));
        assertTrue(result.stream().anyMatch(v -> "京C11111".equals(v.getPlateNumber())));
        assertFalse(result.stream().anyMatch(v -> "京B67890".equals(v.getPlateNumber())));
        
        // 验证调用了车辆信息查询
        verify(vehicleInfoService).selectExistingLicensePlates(Arrays.asList("京A12345", "京B67890", "京C11111"));
    }

    @Test
    void testFilterVehicles_FilterEnabled_NoRegisteredVehicles_ShouldReturnEmpty() {
        // 配置过滤功能启用
        when(configService.selectConfigByKey("unmanaged.vehicle.filter.enabled"))
            .thenReturn("true");

        // 模拟没有车辆在数据库中存在
        when(vehicleInfoService.selectExistingLicensePlates(anyList()))
            .thenReturn(Collections.emptyList());

        // 执行过滤
        List<AirportVehicle> result = vehicleLocationFilter.filterVehicles(testVehicles);

        // 验证结果：应该返回空列表
        assertEquals(0, result.size());
    }

    @Test
    void testFilterVehicles_EmptyInput_ShouldReturnEmpty() {
        // 执行过滤
        List<AirportVehicle> result = vehicleLocationFilter.filterVehicles(Collections.emptyList());

        // 验证结果
        assertEquals(0, result.size());
        
        // 验证没有调用任何服务
        verify(configService, never()).selectConfigByKey(any());
        verify(vehicleInfoService, never()).selectExistingLicensePlates(any());
    }

    @Test
    void testFilterVehicles_NullInput_ShouldReturnNull() {
        // 执行过滤
        List<AirportVehicle> result = vehicleLocationFilter.filterVehicles(null);

        // 验证结果
        assertNull(result);
        
        // 验证没有调用任何服务
        verify(configService, never()).selectConfigByKey(any());
        verify(vehicleInfoService, never()).selectExistingLicensePlates(any());
    }

    @Test
    void testFilterVehicles_ConfigServiceException_ShouldReturnAllVehicles() {
        // 配置服务抛出异常
        when(configService.selectConfigByKey("unmanaged.vehicle.filter.enabled"))
            .thenThrow(new RuntimeException("配置服务异常"));

        // 执行过滤
        List<AirportVehicle> result = vehicleLocationFilter.filterVehicles(testVehicles);

        // 验证结果：异常时应该返回所有车辆（降级处理）
        assertEquals(4, result.size());
        assertEquals(testVehicles, result);
        
        // 验证没有调用车辆信息查询
        verify(vehicleInfoService, never()).selectExistingLicensePlates(any());
    }

    @Test
    void testFilterVehicles_VehicleInfoServiceException_ShouldReturnAllVehicles() {
        // 配置过滤功能启用
        when(configService.selectConfigByKey("unmanaged.vehicle.filter.enabled"))
            .thenReturn("true");

        // 车辆信息服务抛出异常
        when(vehicleInfoService.selectExistingLicensePlates(anyList()))
            .thenThrow(new RuntimeException("数据库查询异常"));

        // 执行过滤
        List<AirportVehicle> result = vehicleLocationFilter.filterVehicles(testVehicles);

        // 验证结果：异常时应该返回所有车辆（降级处理）
        assertEquals(4, result.size());
        assertEquals(testVehicles, result);
    }

    @Test
    void testFilterVehicles_AllVehiclesWithoutPlateNumber_ShouldReturnEmpty() {
        // 准备没有车牌号的车辆数据
        AirportVehicle vehicleNoPlate1 = AirportVehicle.builder()
            .objectId("")
            .objectName("")
            .build();

        AirportVehicle vehicleNoPlate2 = AirportVehicle.builder()
            .objectId(null)
            .objectName(null)
            .build();

        List<AirportVehicle> vehiclesWithoutPlates = Arrays.asList(vehicleNoPlate1, vehicleNoPlate2);

        // 配置过滤功能启用
        when(configService.selectConfigByKey("unmanaged.vehicle.filter.enabled"))
            .thenReturn("true");

        // 执行过滤
        List<AirportVehicle> result = vehicleLocationFilter.filterVehicles(vehiclesWithoutPlates);

        // 验证结果：没有车牌号的车辆应该被过滤掉
        assertEquals(0, result.size());
        
        // 验证没有调用车辆信息查询（因为没有有效的车牌号）
        verify(vehicleInfoService, never()).selectExistingLicensePlates(any());
    }

    @Test
    void testFilterVehicles_ConfigValueCaseInsensitive() {
        // 测试配置值大小写不敏感
        when(configService.selectConfigByKey("unmanaged.vehicle.filter.enabled"))
            .thenReturn("TRUE"); // 大写

        when(vehicleInfoService.selectExistingLicensePlates(anyList()))
            .thenReturn(Arrays.asList("京A12345"));

        // 执行过滤
        List<AirportVehicle> result = vehicleLocationFilter.filterVehicles(testVehicles);

        // 验证结果：应该启用过滤
        assertEquals(1, result.size());
        assertEquals("京A12345", result.get(0).getPlateNumber());
    }
}