package com.ruoyi.web.modules.video.service.impl;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.Mockito.when;

import org.junit.jupiter.api.DisplayName;
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.ruoyi.common.exception.ServiceException;
import com.ruoyi.web.modules.video.LivestockMonitorTestSamples;
import com.ruoyi.web.modules.video.domain.BizLivestockMonitor;
import com.ruoyi.web.modules.video.domain.dto.OpenCameraPlayUrlDto;
import com.ruoyi.web.modules.video.domain.vo.LivestockMonitorPlayUrlVo;
import com.ruoyi.web.modules.video.mapper.BizLivestockMonitorMapper;
import com.ruoyi.web.modules.video.support.LivestockMonitorRules;
import com.ruoyi.web.modules.video.support.ThirdPartyCameraClient;

@ExtendWith(MockitoExtension.class)
@DisplayName("畜牧监控播放地址")
class BizLivestockMonitorPlayUrlTest
{
    @Mock
    private BizLivestockMonitorMapper bizLivestockMonitorMapper;

    @Mock
    private ThirdPartyCameraClient thirdPartyCameraClient;

    @InjectMocks
    private BizLivestockMonitorServiceImpl service;

    @Test
    @DisplayName("按 monitorId 拉取播放地址")
    void getPlayUrlByMonitorId()
    {
        BizLivestockMonitor row = LivestockMonitorTestSamples.samplePastureMonitor();
        row.setId(1L);
        when(bizLivestockMonitorMapper.selectBizLivestockMonitorByMonitorId("50001")).thenReturn(row);
        OpenCameraPlayUrlDto dto = new OpenCameraPlayUrlDto();
        dto.setCameraId(50001L);
        dto.setVideoSource("wvp");
        dto.setVideoUrl("https://example.com/live/50001.m3u8");
        when(thirdPartyCameraClient.fetchPlayUrl(50001L)).thenReturn(dto);
        LivestockMonitorPlayUrlVo vo = service.getPlayUrlByMonitorId("50001");
        assertEquals("50001", vo.getMonitorId());
        assertEquals("https://example.com/live/50001.m3u8", vo.getVideoUrl());
        assertEquals("WVP", vo.getVideoSourceName());
    }

    @Test
    @DisplayName("monitorId 不存在")
    void getPlayUrlNotFound()
    {
        when(bizLivestockMonitorMapper.selectBizLivestockMonitorByMonitorId("99999")).thenReturn(null);
        assertThrows(ServiceException.class, () -> service.getPlayUrlByMonitorId("99999"));
    }

    @Test
    @DisplayName("已删除监控不可预览")
    void getPlayUrlDeleted()
    {
        BizLivestockMonitor row = LivestockMonitorTestSamples.samplePastureMonitor();
        row.setDelFlag(LivestockMonitorRules.DEL_FLAG_DELETED);
        when(bizLivestockMonitorMapper.selectBizLivestockMonitorByMonitorId("50001")).thenReturn(row);
        assertThrows(ServiceException.class, () -> service.getPlayUrlByMonitorId("50001"));
    }
}
