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

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import java.util.Date;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.ArgumentCaptor;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import com.ruoyi.web.modules.industryservice.domain.BizPasture;
import com.ruoyi.web.modules.industryservice.mapper.BizPastureMapper;
import com.ruoyi.web.modules.video.domain.BizLivestockMonitor;
import com.ruoyi.web.modules.video.domain.dto.OpenCameraDto;
import com.ruoyi.web.modules.video.mapper.BizLivestockMonitorMapper;
import com.ruoyi.web.modules.video.support.LivestockMonitorRules;

@ExtendWith(MockitoExtension.class)
@DisplayName("畜牧监控同步落库")
class LivestockMonitorSyncTxServiceTest
{
    @Mock
    private BizLivestockMonitorMapper bizLivestockMonitorMapper;

    @Mock
    private BizPastureMapper bizPastureMapper;

    @InjectMocks
    private LivestockMonitorSyncTxService livestockMonitorSyncTxService;

    @Test
    @DisplayName("farmName 匹配牧场时写入 owner_type=1 与 owner_id")
    void persistCameraResolvesPastureOwner()
    {
        OpenCameraDto dto = new OpenCameraDto();
        dto.setCameraId(50001L);
        dto.setDeviceName("1号圈舍监控");
        dto.setFarmName("示范牧场");
        dto.setVideoSource("wvp");
        when(bizLivestockMonitorMapper.selectBizLivestockMonitorByMonitorId("50001")).thenReturn(null);
        BizPasture pasture = new BizPasture();
        pasture.setId(101L);
        pasture.setPastureName("示范牧场");
        when(bizPastureMapper.selectBizPastureByExactName("示范牧场")).thenReturn(pasture);
        when(bizLivestockMonitorMapper.insertBizLivestockMonitor(any())).thenReturn(1);

        boolean inserted = livestockMonitorSyncTxService.persistCamera(dto, new Date(), "admin");
        assertEquals(true, inserted);

        ArgumentCaptor<BizLivestockMonitor> captor = ArgumentCaptor.forClass(BizLivestockMonitor.class);
        verify(bizLivestockMonitorMapper).insertBizLivestockMonitor(captor.capture());
        BizLivestockMonitor row = captor.getValue();
        assertEquals(LivestockMonitorRules.OWNER_TYPE_PASTURE, row.getOwnerType());
        assertEquals(101L, row.getOwnerId());
        assertEquals("示范牧场", row.getOwnerName());
    }

    @Test
    @DisplayName("farmName 未匹配牧场时仅保留 owner_name 文本")
    void persistCameraFarmNameWithoutPasture()
    {
        OpenCameraDto dto = new OpenCameraDto();
        dto.setCameraId(50002L);
        dto.setDeviceName("入口监控");
        dto.setFarmName("未知牧场");
        dto.setVideoSource("ys7");
        when(bizLivestockMonitorMapper.selectBizLivestockMonitorByMonitorId("50002")).thenReturn(null);
        when(bizPastureMapper.selectBizPastureByExactName("未知牧场")).thenReturn(null);
        when(bizLivestockMonitorMapper.insertBizLivestockMonitor(any())).thenReturn(1);

        livestockMonitorSyncTxService.persistCamera(dto, new Date(), "admin");

        ArgumentCaptor<BizLivestockMonitor> captor = ArgumentCaptor.forClass(BizLivestockMonitor.class);
        verify(bizLivestockMonitorMapper).insertBizLivestockMonitor(captor.capture());
        BizLivestockMonitor row = captor.getValue();
        assertNull(row.getOwnerType());
        assertNull(row.getOwnerId());
        assertEquals("未知牧场", row.getOwnerName());
    }
}
