package com.zcloud.primeport.command.query;

import com.alibaba.cola.dto.PageResponse;
import com.zcloud.primeport.command.convertor.VideoCoConvertor;
import com.zcloud.primeport.dto.VideoPageQry;
import com.zcloud.primeport.dto.clientobject.VideoCO;
import com.zcloud.primeport.persistence.dataobject.VideoDO;
import com.zcloud.primeport.persistence.repository.VideoRepository;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;

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

import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyMap;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

@ExtendWith(MockitoExtension.class)
class VideoQueryExeTest {

    @Mock
    private VideoRepository videoRepository;

    @Mock
    private VideoCoConvertor videoCoConvertor;

    private VideoQueryExe videoQueryExe;

    @BeforeEach
    void setUp() {
        videoQueryExe = new VideoQueryExe(videoRepository, videoCoConvertor);
    }

    @Test
    void shouldCallListPageWithOnlineAndReturnIsOnlineAndType() {
        VideoPageQry qry = new VideoPageQry();
        qry.setEqIsOnline(1);
        qry.setPageSize(10);
        qry.setPageIndex(1);

        VideoDO videoDO = new VideoDO();
        videoDO.setId(1L);
        videoDO.setVideoResourceName("test-camera");
        videoDO.setIsOnline("1");
        videoDO.setType("yd");

        PageResponse<VideoDO> doPage = PageResponse.of(
                Collections.singletonList(videoDO), 1, 10, 1);

        VideoCO videoCO = new VideoCO();
        videoCO.setId(1L);
        videoCO.setVideoResourceName("test-camera");
        videoCO.setIsOnline("1");
        videoCO.setType("yd");

        when(videoRepository.listPageWithOnline(anyMap())).thenReturn(doPage);
        when(videoCoConvertor.converDOsToCOs(any())).thenReturn(Collections.singletonList(videoCO));

        PageResponse<VideoCO> result = videoQueryExe.execute(qry);

        assertNotNull(result);
        assertEquals(1, result.getTotalCount());
        List<VideoCO> data = result.getData();
        assertEquals(1, data.size());
        assertEquals("test-camera", data.get(0).getVideoResourceName());
        assertEquals("1", data.get(0).getIsOnline());
        assertEquals("yd", data.get(0).getType());

        verify(videoRepository).listPageWithOnline(anyMap());
    }

    @Test
    void shouldReturnEmptyListWhenNoData() {
        VideoPageQry qry = new VideoPageQry();
        qry.setPageSize(10);
        qry.setPageIndex(1);

        PageResponse<VideoDO> doPage = PageResponse.of(Collections.emptyList(), 0, 10, 1);

        when(videoRepository.listPageWithOnline(anyMap())).thenReturn(doPage);
        when(videoCoConvertor.converDOsToCOs(any())).thenReturn(Collections.emptyList());

        PageResponse<VideoCO> result = videoQueryExe.execute(qry);

        assertNotNull(result);
        assertEquals(0, result.getTotalCount());
        assertTrue(result.getData().isEmpty());
    }
}
