package de.thpeetz.kontor.comics.repository;

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

import java.util.List;
import java.util.stream.Collectors;

import de.thpeetz.kontor.comics.data.*;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest
class ComicRepositoryTest {

    @Autowired
    private ComicRepository comicRepository;

    @Autowired
    private PublisherRepository publisherRepository;

    @Test
    void testFindByTitle() {
        List<Comic> comics = comicRepository.findByTitle("Emma Frost");
        assertEquals(1, comics.size());
        assertEquals("Emma Frost", comics.get(0).getTitle());
        Publisher publisher = comics.get(0).getPublisher();
        assertEquals("Marvel", publisher.getName());
    }

    @Test
    void testFindByTitleIgnoreCase() {
        List<Comic> comics = comicRepository.findByTitleIgnoreCase("x-men");
        assertEquals(1, comics.size());
        assertEquals("X-Men", comics.get(0).getTitle());
    }
    @Test
    void testFindByTitleAndPublisher() {
        Publisher publisher = publisherRepository.findByName("Marvel");
        assertNotNull(publisher);
        Comic found = comicRepository.findByTitleAndPublisher("Emma Frost", publisher);
        assertNotNull(found);
        assertEquals("Emma Frost", found.getTitle());
        assertEquals("Marvel", found.getPublisher().getName());

    }

    @Test
    void testSearch() {
        List<Comic> comics = comicRepository.search("X-men");
        assertEquals(11, comics.size());
        assertTrue(comics.stream().map(comic -> comic.getTitle()).collect(Collectors.toList()).contains("Astonishing X-Men"));
    }
}
