package com.zsc.edu.gateway.service.notice;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zsc.edu.gateway.domain.message.NoticeBuilder;
import com.zsc.edu.gateway.framework.security.UserDetailsImpl;
import com.zsc.edu.gateway.modules.message.dto.UserNoticeDto;
import com.zsc.edu.gateway.modules.message.entity.Notice;
import com.zsc.edu.gateway.modules.message.entity.NoticeType;
import com.zsc.edu.gateway.modules.message.repo.NoticeRepository;
import com.zsc.edu.gateway.modules.message.repo.UserNoticeRepository;
import com.zsc.edu.gateway.modules.message.service.UserNoticeService;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.Arrays;
import java.util.HashSet;
import java.util.List;

import static org.junit.jupiter.api.Assertions.*;

@SpringBootTest
public class NoticeServiceTest {
    @Autowired
    public NoticeRepository noticeRepository;
    @Autowired
    public UserNoticeRepository userNoticeRepository;
    @Autowired
    public UserNoticeService service;

    private Notice notice1;

    @BeforeEach
    void setup() {
        UserDetailsImpl userDetails = new UserDetailsImpl();
        userDetails.setUsername("admin");
        notice1 = NoticeBuilder.bMessage().type(NoticeType.MESSAGE).title("A测试消息1").build();
        noticeRepository.insert(notice1);

    }

    @AfterEach
    void tearDown() {
        List<String> titlesToDelete = Arrays.asList("A测试消息1", "A测试消息3");
        noticeRepository.delete(new QueryWrapper<Notice>().in("title", titlesToDelete));
    }

    @Test
    void list() {
        LambdaQueryWrapper<Notice> queryWrapper = new LambdaQueryWrapper<>();
        assertEquals(1, noticeRepository.selectList(queryWrapper.like(Notice::getTitle, "A测试")).size());
        assertEquals(1, noticeRepository.selectList(queryWrapper.eq(Notice::getTitle, notice1.getTitle())).size());
//        assertEquals(2, messageRepository.selectList(null).size());
    }

    @Test
    void createMessage() {
        UserNoticeDto dto = new UserNoticeDto(new HashSet<>(Arrays.asList(1L, 2L)), NoticeType.MESSAGE, false, false, false, "A测试消息3", "测试测试");
        service.createByAdmin(dto);
        Notice notice = noticeRepository.selectOne(new LambdaQueryWrapper<Notice>().eq(Notice::getTitle, dto.getTitle()));
        assertNotNull(notice.getId());
    }
}
