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

import com.ycwl.basic.biz.FaceStatusManager;
import com.ycwl.basic.enums.FaceCutStatus;
import com.ycwl.basic.face.pipeline.core.FaceMatchingContext;
import com.ycwl.basic.pipeline.core.StageResult;
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.ArgumentMatchers.anyLong;
import static org.mockito.Mockito.*;

/**
 * SetTaskStatusStage 单元测试
 */
@ExtendWith(MockitoExtension.class)
class SetTaskStatusStageTest {

    @InjectMocks
    private SetTaskStatusStage stage;

    @Mock
    private FaceStatusManager faceStatusManager;

    private FaceMatchingContext context;

    @BeforeEach
    void setUp() {
        context = FaceMatchingContext.forAutoMatching(1L, true); // isNew=true
    }

    @Test
    void testExecute_NewUser_Success() {
        // Given
        context = FaceMatchingContext.forAutoMatching(1L, true);

//        doNothing().when(taskStatusBiz).setFaceCutStatus(1L, 0);

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

        // Then
        assertTrue(result.isSuccess());
        assertTrue(result.getMessage().contains("任务状态已设置"));
        verify(faceStatusManager, times(1)).setFaceCutStatus(1L, FaceCutStatus.CUTTING);
    }

    @Test
    void testExecute_OldUser_Skipped() {
        // Given: 老用户
        context = FaceMatchingContext.forAutoMatching(1L, false); // isNew=false

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

        // Then
        assertTrue(result.isSkipped());
        assertTrue(result.getMessage().contains("非新用户"));
        verify(faceStatusManager, never()).setFaceCutStatus(anyLong(), any(FaceCutStatus.class));
    }

    @Test
    void testExecute_SetStatusFailed_Degraded() {
        // Given
        context = FaceMatchingContext.forAutoMatching(1L, true);

        doThrow(new RuntimeException("Database error"))
                .when(faceStatusManager).setFaceCutStatus(1L, FaceCutStatus.CUTTING);

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

        // Then
        assertTrue(result.isDegraded()); // 降级处理，不影响主流程
        assertTrue(result.getMessage().contains("任务状态设置失败"));
        verify(faceStatusManager, times(1)).setFaceCutStatus(1L, FaceCutStatus.CUTTING);
    }

    @Test
    void testExecute_DifferentFaceId() {
        // Given: 不同的faceId
        context = FaceMatchingContext.forAutoMatching(999L, true);

//        doNothing().when(taskStatusBiz).setFaceCutStatus(999L, 0);

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

        // Then
        assertTrue(result.isSuccess());
        verify(faceStatusManager, times(1)).setFaceCutStatus(999L, FaceCutStatus.CUTTING);
    }

    @Test
    void testExecute_RecognitionOnlyScene_Skipped() {
        // Given: 仅识别场景（非新用户场景）
        context = FaceMatchingContext.forRecognitionOnly(1L);

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

        // Then
        assertTrue(result.isSkipped());
        verify(faceStatusManager, never()).setFaceCutStatus(anyLong(), any(FaceCutStatus.class));
    }

    @Test
    void testExecute_CustomMatchingOldUser_Skipped() {
        // Given: 自定义匹配老用户 (需要提供faceSampleIds)
        context = FaceMatchingContext.forCustomMatching(1L, Arrays.asList(101L, 102L));
        // CustomMatching默认isNew=false

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

        // Then
        assertTrue(result.isSkipped());
        verify(faceStatusManager, never()).setFaceCutStatus(anyLong(), any(FaceCutStatus.class));
    }

    @Test
    void testExecute_NullPointerException_Degraded() {
        // Given
        context = FaceMatchingContext.forAutoMatching(1L, true);

        doThrow(new NullPointerException("Null task status"))
                .when(faceStatusManager).setFaceCutStatus(1L, FaceCutStatus.CUTTING);

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

        // Then
        assertTrue(result.isDegraded());
        assertTrue(result.getMessage().contains("任务状态设置失败"));
        verify(faceStatusManager, times(1)).setFaceCutStatus(1L, FaceCutStatus.CUTTING);
    }

    @Test
    void testShouldExecute_NewUser_True() {
        // Given
        context = FaceMatchingContext.forAutoMatching(1L, true);

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

        // Then
        assertFalse(result.isSkipped()); // 应该执行
        verify(faceStatusManager, times(1)).setFaceCutStatus(1L, FaceCutStatus.CUTTING);
    }

    @Test
    void testShouldExecute_OldUser_False() {
        // Given
        context = FaceMatchingContext.forAutoMatching(1L, false);

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

        // Then
        assertTrue(result.isSkipped()); // 应该跳过
        verify(faceStatusManager, never()).setFaceCutStatus(anyLong(), any(FaceCutStatus.class));
    }
}
