package com.ycwl.basic.face.pipeline.stages;

import com.ycwl.basic.face.pipeline.core.FaceMatchingContext;
import com.ycwl.basic.pipeline.core.StageResult;
import com.ycwl.basic.face.pipeline.enums.FaceMatchingScene;
import com.ycwl.basic.model.pc.face.entity.FaceEntity;
import com.ycwl.basic.service.pc.helper.FaceMetricsRecorder;
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 java.util.Arrays;

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

/**
 * RecordCustomMatchMetricsStage 单元测试
 */
@ExtendWith(MockitoExtension.class)
class RecordCustomMatchMetricsStageTest {

    @Mock
    private FaceMetricsRecorder metricsRecorder;

    @InjectMocks
    private RecordCustomMatchMetricsStage stage;

    private FaceMatchingContext context;
    private FaceEntity face;

    @BeforeEach
    void setUp() {
        face = new FaceEntity();
        face.setId(1L);
        face.setMemberId(100L);
        face.setScenicId(10L);
    }

    @Test
    void testExecute_CustomMatchingScene_Success() {
        // Given: 自定义匹配场景
        context = FaceMatchingContext.forCustomMatching(1L, Arrays.asList(101L, 102L));
        context.setFace(face);

        // When
        StageResult<FaceMatchingContext> result = stage.execute(context);

        // Then
        assertTrue(result.isSuccess());
        assertTrue(result.getMessage().contains("自定义匹配指标记录完成"));
        assertEquals(FaceMatchingScene.CUSTOM_MATCHING, context.getScene());
        verify(metricsRecorder, times(1)).recordCustomMatchCount(1L);
    }

    @Test
    void testExecute_AutoMatchingScene_Skipped() {
        // Given: 自动匹配场景
        context = FaceMatchingContext.forAutoMatching(1L, true);
        context.setFace(face);

        // When
        StageResult<FaceMatchingContext> result = stage.execute(context);

        // Then
        assertTrue(result.isSkipped());
        assertEquals(FaceMatchingScene.AUTO_MATCHING, context.getScene());
        verify(metricsRecorder, never()).recordCustomMatchCount(anyLong());
    }

    @Test
    void testExecute_RecordFailed_Degraded() {
        // Given: 自定义匹配场景,但记录失败
        context = FaceMatchingContext.forCustomMatching(1L, Arrays.asList(101L, 102L));
        context.setFace(face);

        doThrow(new RuntimeException("Metrics record error"))
                .when(metricsRecorder).recordCustomMatchCount(1L);

        // When
        StageResult<FaceMatchingContext> result = stage.execute(context);

        // Then
        assertTrue(result.isDegraded());
        assertTrue(result.getMessage().contains("指标记录失败"));
        verify(metricsRecorder, times(1)).recordCustomMatchCount(1L);
    }

    @Test
    void testExecute_DifferentFaceId() {
        // Given: 不同的faceId
        context = FaceMatchingContext.forCustomMatching(888L, Arrays.asList(101L, 102L));
        face.setId(888L);
        context.setFace(face);

        // When
        StageResult<FaceMatchingContext> result = stage.execute(context);

        // Then
        assertTrue(result.isSuccess());
        verify(metricsRecorder, times(1)).recordCustomMatchCount(888L);
    }

    @Test
    void testExecute_NullPointerException_Degraded() {
        // Given: 空指针异常
        context = FaceMatchingContext.forCustomMatching(1L, Arrays.asList(101L, 102L));
        context.setFace(face);

        doThrow(new NullPointerException("Null recorder"))
                .when(metricsRecorder).recordCustomMatchCount(1L);

        // When
        StageResult<FaceMatchingContext> result = stage.execute(context);

        // Then
        assertTrue(result.isDegraded());
        assertTrue(result.getMessage().contains("指标记录失败"));
    }

    @Test
    void testExecute_MultipleFaceSamples() {
        // Given: 多个faceSample
        context = FaceMatchingContext.forCustomMatching(
                1L,
                Arrays.asList(101L, 102L, 103L, 104L, 105L));
        context.setFace(face);

        // When
        StageResult<FaceMatchingContext> result = stage.execute(context);

        // Then
        assertTrue(result.isSuccess());
        verify(metricsRecorder, times(1)).recordCustomMatchCount(1L);
    }

    @Test
    void testExecute_SingleFaceSample() {
        // Given: 单个faceSample
        context = FaceMatchingContext.forCustomMatching(1L, Arrays.asList(101L));
        context.setFace(face);

        // When
        StageResult<FaceMatchingContext> result = stage.execute(context);

        // Then
        assertTrue(result.isSuccess());
        verify(metricsRecorder, times(1)).recordCustomMatchCount(1L);
    }
}
