package de.thpeetz.kontor.comics.repository;

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

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

import de.thpeetz.kontor.comics.services.ComicService;

@SpringBootTest
@TestMethodOrder(OrderAnnotation.class)
class ComicWorkRepositoryTest {

    @Autowired
    private ComicWorkRepository comicWorkRepository;

    @Autowired
    private ComicRepository comicRepository;

    @Autowired
    private WorktypeRepository worktypeRepository;

    @Autowired
    private ArtistRepository artistRepository;

    @Autowired
    private ComicService comicService;

    @Test
    @Order(1)
    void checkInitialLoad() {
        int count = comicWorkRepository.findAll().size();
        assertEquals(18, count);
    }

    @Test
    @Order(2)
    void saveComicWork() {
        int count = comicWorkRepository.findAll().size();
        Artist artist = artistRepository.findByName("Turner, Michael");
        Worktype worktype = worktypeRepository.findByName("Writer");
        Comic comic = comicRepository.findByTitle("Emma Frost").get(0);
        ComicWork comicWork = new ComicWork();
        comicWork.setArtist(artist);
        comicWork.setComic(comic);
        comicWork.setWorkType(worktype);
        comicWorkRepository.save(comicWork);
        assertEquals(count + 1, comicWorkRepository.findAll().size());
    }

    @Test
    @Order(3)
    void findByComicAndArtistAndComicWork() {
        assertEquals(19, comicWorkRepository.count());
        Artist artist = artistRepository.findByName("Turner, Michael");
        Worktype worktype = worktypeRepository.findByName("Writer");
        Comic comic = comicRepository.findByTitle("Emma Frost").get(0);
        ComicWork comicWork = comicWorkRepository.findbyComicAndArtistAndWorktype(comic, artist, worktype);
        assertNotNull(comicWork);
        assertEquals(comicWork.getArtist().getName(), artist.getName());
        assertEquals(comicWork.getComic().getTitle(), comic.getTitle());
        assertEquals(comicWork.getWorkType().getName(), worktype.getName());
        ComicWork notFound = comicWorkRepository.findbyComicAndArtistAndWorktype(comic, artist, null);
        assertNull(notFound);
    }

    @Test
    @Order(4)
    void deleteComicWork() {
        long count = comicWorkRepository.count();
        Artist artist = artistRepository.findByName("Turner, Michael");
        Worktype worktype = worktypeRepository.findByName("Writer");
        Comic comic = comicRepository.findByTitle("Emma Frost").get(0);
        ComicWork comicWork = comicWorkRepository.findbyComicAndArtistAndWorktype(comic, artist, worktype);
        assertNotNull(comicWork);
        comicService.deleteComicWork(comicWork);
        assertEquals(count - 1, comicWorkRepository.count());
    }
}
