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.BulletinBuilder;
import com.zsc.edu.gateway.exception.ConstraintException;
import com.zsc.edu.gateway.framework.security.UserDetailsImpl;
import com.zsc.edu.gateway.modules.message.dto.BulletinDto;
import com.zsc.edu.gateway.modules.message.entity.Bulletin;
import com.zsc.edu.gateway.modules.message.repo.BulletinRepository;
import com.zsc.edu.gateway.modules.message.service.BulletinService;
import jakarta.annotation.Resource;
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.List;

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

@SpringBootTest
public class BulletinServiceTest {

    @Resource
    private BulletinService service;
    @Autowired
    private BulletinRepository repo;

    private Bulletin bulletin1;
    private Bulletin bulletin2;
    private UserDetailsImpl userDetails;

    @BeforeEach
    void setUp() {
        userDetails = new UserDetailsImpl();
        userDetails.setUsername("admin");
        bulletin1 = createAndInsertBulletin("测试1");
        bulletin2 = createAndInsertBulletin("测试2");
    }

    @AfterEach
    void tearDown() {
        // 清理所有测试公告
        List<String> titlesToDelete = Arrays.asList("测试1", "测试2", "测试3", "测试");
        repo.delete(new QueryWrapper<Bulletin>().in("title", titlesToDelete));
    }

    private Bulletin createAndInsertBulletin(String title) {
        Bulletin bulletin = BulletinBuilder.bBulletin().top(false).title(title).build();
        repo.insert(bulletin);
        return bulletin;
    }

    @Test
    void list() {
        LambdaQueryWrapper<Bulletin> queryWrapper = new LambdaQueryWrapper<>();
        assertEquals(2, service.list(queryWrapper.like(Bulletin::getTitle, "测试")).size());
        assertEquals(1, service.list(queryWrapper.eq(Bulletin::getTitle, bulletin1.getTitle())).size());
//        assertEquals(2, service.list().size());
    }

    @Test
    void createBulletin() {
        BulletinDto dto = createBulletinDto("测试", true, "测试测试", "测试公告增加");
        Bulletin bulletin = service.create(userDetails, dto);
        assertNotNull(bulletin.getId());
        assertThrows(ConstraintException.class, () -> service.create(userDetails, createBulletinDto(bulletin2.getTitle(), false, "", "")));
    }

    @Test
    void updateBulletin() {
        BulletinDto dto = createBulletinDto("测试3", true, "测试更新", "测试公告更新");
        assertTrue(service.update(userDetails, dto, bulletin2.getId()));
        Bulletin updatedBulletin = service.getOne(new LambdaQueryWrapper<Bulletin>()
                .eq(Bulletin::getTitle, dto.getTitle()));
        assertNotNull(updatedBulletin);
        assertEquals(dto.getTitle(), updatedBulletin.getTitle());
        assertEquals(bulletin2.getId(), updatedBulletin.getId());
    }


    private BulletinDto createBulletinDto(String title, boolean top, String content, String remark) {
        BulletinDto dto = new BulletinDto();
        dto.setTitle(title);
        dto.setTop(top);
        dto.setContent(content);
        dto.setRemark(remark);
        return dto;
    }
}
