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

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.when;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
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.category.domain.vo.InformationCategoryItemVo;
import com.ruoyi.web.modules.farming.support.TechResourceRules;
import com.ruoyi.web.modules.technology.OnlineTrainingCourseTestSamples;
import com.ruoyi.web.modules.technology.domain.OnlineTrainingCourseDetailVo;
import com.ruoyi.web.modules.technology.domain.OnlineTrainingCourseListVo;
import com.ruoyi.web.modules.technology.domain.OnlineTrainingCourseQuery;
import com.ruoyi.web.modules.technology.mapper.BizOnlineTrainingCourseMapper;
import com.ruoyi.web.modules.technology.support.OnlineTrainingCourseRules;

@ExtendWith(MockitoExtension.class)
@DisplayName("BizOnlineTrainingCourseServiceImpl")
class BizOnlineTrainingCourseServiceImplTest
{
    @Mock
    private BizOnlineTrainingCourseMapper onlineTrainingCourseMapper;

    @InjectMocks
    private BizOnlineTrainingCourseServiceImpl service;

    @Test
    @DisplayName("列表 Vo 映射")
    void listMapping()
    {
        when(onlineTrainingCourseMapper.selectOnlineTrainingCourseList(any()))
                .thenReturn(Collections.singletonList(OnlineTrainingCourseTestSamples.samplePublishedRow()));
        OnlineTrainingCourseListVo vo = service.selectOnlineTrainingCourseList(new OnlineTrainingCourseQuery())
                .get(0);
        assertEquals("基础养殖入门", vo.getCourseTitle());
        assertEquals("课程简介正文", vo.getCourseIntro());
        assertEquals(TechResourceRules.COURSE_TOPIC_BASIC, vo.getCourseTopic());
        assertEquals("course.mp4", vo.getVideoFileName());
    }

    @Test
    @DisplayName("详情 Vo 映射")
    void detailMapping()
    {
        when(onlineTrainingCourseMapper.selectOnlineTrainingCourseById(101L))
                .thenReturn(OnlineTrainingCourseTestSamples.samplePublishedRow());
        OnlineTrainingCourseDetailVo vo = service.selectOnlineTrainingCourseById(101L);
        assertEquals(101L, vo.getId());
        assertEquals(OnlineTrainingCourseTestSamples.VIDEO_URL, vo.getVideoFileUrl());
        assertEquals("cover.jpg", vo.getCoverFilePath().substring(vo.getCoverFilePath().lastIndexOf('/') + 1));
    }

    @Test
    @DisplayName("详情不存在")
    void detailNotFound()
    {
        when(onlineTrainingCourseMapper.selectOnlineTrainingCourseById(9L)).thenReturn(null);
        ServiceException ex = assertThrows(ServiceException.class, () -> service.selectOnlineTrainingCourseById(9L));
        assertEquals(OnlineTrainingCourseRules.NOT_FOUND_MSG, ex.getMessage());
    }

    @Test
    @DisplayName("课程主题列表")
    void courseTopics()
    {
        when(onlineTrainingCourseMapper.selectDistinctCourseTopics())
                .thenReturn(Arrays.asList(
                        new InformationCategoryItemVo(TechResourceRules.COURSE_TOPIC_BASIC, "基础养殖"),
                        new InformationCategoryItemVo(TechResourceRules.COURSE_TOPIC_DISEASE, "疫病防治")));
        List<InformationCategoryItemVo> topics = service.selectCourseTopics();
        assertEquals(2, topics.size());
        assertEquals(TechResourceRules.COURSE_TOPIC_BASIC, topics.get(0).getCode());
        assertEquals("基础养殖", topics.get(0).getName());
        assertEquals(TechResourceRules.COURSE_TOPIC_DISEASE, topics.get(1).getCode());
        assertEquals("疫病防治", topics.get(1).getName());
    }

    @Test
    @DisplayName("查询条件 trim")
    void queryTrim()
    {
        when(onlineTrainingCourseMapper.selectOnlineTrainingCourseList(any()))
                .thenReturn(Collections.emptyList());
        OnlineTrainingCourseQuery q = new OnlineTrainingCourseQuery();
        q.setCourseTitle("  养殖  ");
        q.setCourseTopic(" " + TechResourceRules.COURSE_TOPIC_BASIC + " ");
        service.selectOnlineTrainingCourseList(q);
    }
}
