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.model.pc.face.entity.FaceEntity;
import com.ycwl.basic.model.pc.source.entity.MemberSourceEntity;
import com.ycwl.basic.service.pc.processor.BuyStatusProcessor;
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.ArrayList;
import java.util.Arrays;
import java.util.List;

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

/**
 * ProcessBuyStatusStage 单元测试
 */
@ExtendWith(MockitoExtension.class)
class ProcessBuyStatusStageTest {

    @Mock
    private BuyStatusProcessor buyStatusProcessor;

    @InjectMocks
    private ProcessBuyStatusStage stage;

    private FaceMatchingContext context;
    private FaceEntity face;

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

        face = new FaceEntity();
        face.setId(1L);
        face.setMemberId(100L);
        face.setScenicId(10L);

        context.setFace(face);
    }

    @Test
    void testExecute_MemberSourceListNull_Skipped() {
        // Given: memberSourceList为null
        context.setMemberSourceList(null);

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

        // Then
        assertTrue(result.isSkipped());
        verify(buyStatusProcessor, never()).processBuyStatus(anyList(), anyList(), anyLong(), anyLong(), anyLong());
    }

    @Test
    void testExecute_MemberSourceListEmpty_Skipped() {
        // Given: memberSourceList为空
        context.setMemberSourceList(new ArrayList<>());

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

        // Then
        assertTrue(result.isSkipped());
        verify(buyStatusProcessor, never()).processBuyStatus(anyList(), anyList(), anyLong(), anyLong(), anyLong());
    }

    @Test
    void testExecute_Success_WithFreeSource() {
        // Given: 有免费源文件
        List<MemberSourceEntity> memberSourceList = createMemberSourceList(5);
        context.setMemberSourceList(memberSourceList);

        List<Long> freeSourceIds = Arrays.asList(1L, 2L, 3L);
        context.setFreeSourceIds(freeSourceIds);

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

        // Then
        assertTrue(result.isSuccess());
        assertTrue(result.getMessage().contains("购买状态处理完成"));
        verify(buyStatusProcessor, times(1))
                .processBuyStatus(memberSourceList, freeSourceIds, 100L, 10L, 1L);
    }

    @Test
    void testExecute_Success_NoFreeSource() {
        // Given: 没有免费源文件(freeSourceIds为空)
        List<MemberSourceEntity> memberSourceList = createMemberSourceList(5);
        context.setMemberSourceList(memberSourceList);

        List<Long> freeSourceIds = new ArrayList<>();
        context.setFreeSourceIds(freeSourceIds);

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

        // Then
        assertTrue(result.isSuccess());
        verify(buyStatusProcessor, times(1))
                .processBuyStatus(memberSourceList, freeSourceIds, 100L, 10L, 1L);
    }

    @Test
    void testExecute_Success_NullFreeSource() {
        // Given: freeSourceIds为null
        List<MemberSourceEntity> memberSourceList = createMemberSourceList(5);
        context.setMemberSourceList(memberSourceList);

        context.setFreeSourceIds(null);

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

        // Then
        assertTrue(result.isSuccess());
        verify(buyStatusProcessor, times(1))
                .processBuyStatus(memberSourceList, null, 100L, 10L, 1L);
    }

    @Test
    void testExecute_ProcessorFailed_Degraded() {
        // Given: 处理器执行失败
        List<MemberSourceEntity> memberSourceList = createMemberSourceList(3);
        context.setMemberSourceList(memberSourceList);

        List<Long> freeSourceIds = Arrays.asList(1L);
        context.setFreeSourceIds(freeSourceIds);

        doThrow(new RuntimeException("Processor error"))
                .when(buyStatusProcessor).processBuyStatus(
                        memberSourceList, freeSourceIds, 100L, 10L, 1L);

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

        // Then
        assertTrue(result.isDegraded());
        assertTrue(result.getMessage().contains("购买状态处理失败"));
        verify(buyStatusProcessor, times(1))
                .processBuyStatus(memberSourceList, freeSourceIds, 100L, 10L, 1L);
    }

    @Test
    void testExecute_DifferentMemberId() {
        // Given: 不同的memberId
        face.setMemberId(999L);

        List<MemberSourceEntity> memberSourceList = createMemberSourceList(3);
        context.setMemberSourceList(memberSourceList);

        List<Long> freeSourceIds = Arrays.asList(1L);
        context.setFreeSourceIds(freeSourceIds);

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

        // Then
        assertTrue(result.isSuccess());
        verify(buyStatusProcessor, times(1))
                .processBuyStatus(memberSourceList, freeSourceIds, 999L, 10L, 1L);
    }

    @Test
    void testExecute_DifferentScenicId() {
        // Given: 不同的景区ID
        face.setScenicId(888L);

        List<MemberSourceEntity> memberSourceList = createMemberSourceList(3);
        context.setMemberSourceList(memberSourceList);

        List<Long> freeSourceIds = Arrays.asList(1L);
        context.setFreeSourceIds(freeSourceIds);

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

        // Then
        assertTrue(result.isSuccess());
        verify(buyStatusProcessor, times(1))
                .processBuyStatus(memberSourceList, freeSourceIds, 100L, 888L, 1L);
    }

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

        List<MemberSourceEntity> memberSourceList = createMemberSourceList(3);
        context.setMemberSourceList(memberSourceList);

        List<Long> freeSourceIds = Arrays.asList(1L);
        context.setFreeSourceIds(freeSourceIds);

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

        // Then
        assertTrue(result.isSuccess());
        verify(buyStatusProcessor, times(1))
                .processBuyStatus(memberSourceList, freeSourceIds, 100L, 10L, 777L);
    }

    @Test
    void testExecute_AllSourcesFree() {
        // Given: 所有源文件都免费
        List<MemberSourceEntity> memberSourceList = createMemberSourceList(5);
        context.setMemberSourceList(memberSourceList);

        List<Long> freeSourceIds = Arrays.asList(1L, 2L, 3L, 4L, 5L);
        context.setFreeSourceIds(freeSourceIds);

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

        // Then
        assertTrue(result.isSuccess());
        verify(buyStatusProcessor, times(1))
                .processBuyStatus(memberSourceList, freeSourceIds, 100L, 10L, 1L);
    }

    @Test
    void testExecute_NullPointerException_Degraded() {
        // Given: 空指针异常
        List<MemberSourceEntity> memberSourceList = createMemberSourceList(3);
        context.setMemberSourceList(memberSourceList);

        doThrow(new NullPointerException("Null processor"))
                .when(buyStatusProcessor).processBuyStatus(
                        anyList(), anyList(), anyLong(), anyLong(), anyLong());

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

        // Then
        assertTrue(result.isDegraded());
        assertTrue(result.getMessage().contains("购买状态处理失败"));
    }

    private List<MemberSourceEntity> createMemberSourceList(int count) {
        List<MemberSourceEntity> list = new ArrayList<>();
        for (int i = 0; i < count; i++) {
            MemberSourceEntity entity = new MemberSourceEntity();
            entity.setMemberId(100L);
            entity.setSourceId((long) (i + 1));
            list.add(entity);
        }
        return list;
    }
}
