package de.thpeetz.kontor.bookshelf.data;

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

import java.util.List;

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

@SpringBootTest
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
@Disabled
class BookAuthorRepositoryTest {

    @Autowired
    BookRepository bookRepository;

    @Autowired
    AuthorRepository authorRepository;

    @Autowired
    BookAuthorRepository bookAuthorRepository;

    @Autowired
    BookshelfPublisherRepository bookshelfPublisherRepository;

    @Test
    @Order(1)
    void saveBookAuthor() {
        Author author = new Author();
        author.setFirstName(TestConstants.AUTHOR_FIRSTNAME);
        author.setLastName(TestConstants.AUTHOR_LASTNAME);
        Author savedAuthor = authorRepository.save(author);
        assertEquals(TestConstants.AUTHOR_COUNT+1, authorRepository.findAll().size());
        BookshelfPublisher publisher = new BookshelfPublisher();
        publisher.setName(TestConstants.PUBLISHER_NAME);
        BookshelfPublisher savedPublisher = bookshelfPublisherRepository.save(publisher);
        assertEquals(TestConstants.PUBLISHER_COUNT+1, bookshelfPublisherRepository.findAll().size());
        Book book = new Book();
        book.setTitle(TestConstants.ARTICLE_TITLE);
        book.setIsbn(TestConstants.BOOK_ISBN);
        book.setPublisher(savedPublisher);
        Book savedBook = bookRepository.save(book);
        assertEquals(TestConstants.BOOK_COUNT+1, bookRepository.findAll().size());
        BookAuthor bookAuthor = new BookAuthor();
        bookAuthor.setBook(savedBook);
        bookAuthor.setAuthor(savedAuthor);
        BookAuthor savedBookAuthor = bookAuthorRepository.save(bookAuthor);
        assertEquals(TestConstants.BOOKAUTHOR_COUNT+1, bookAuthorRepository.findAll().size());
    }

    @Test
    @Order(2)
    void testFindByBook() {
        List<BookAuthor> bookAuthors = bookAuthorRepository.findAll();
        assertEquals(1, bookAuthors.size());
        Book book = bookAuthors.get(0).getBook();
        assertEquals(1, bookAuthorRepository.findByBook(book).size());
    }

    @Test
    @Order(3)
    void testFindByAuthor() {
        List<BookAuthor> bookAuthors = bookAuthorRepository.findAll();
        assertEquals(1, bookAuthors.size());
        Author author = bookAuthors.get(0).getAuthor();
        assertEquals(1, bookAuthorRepository.findByAuthor(author).size());
    }

    @Test
    @Order(4)
    void deleteBookAuthor() {
        BookAuthor bookAuthor = bookAuthorRepository.findAll().get(0);
        Author author = bookAuthor.getAuthor();
        author.getBookAuthors().remove(bookAuthor);
        author = authorRepository.save(author);
        Book book = bookAuthor.getBook();
        book.getAuthors().remove(bookAuthor);
        BookshelfPublisher publisher = book.getPublisher();
        publisher.getBooks().remove(book);
        bookshelfPublisherRepository.save(publisher);
        book = bookRepository.save(book);
        bookshelfPublisherRepository.delete(publisher);
        assertEquals(TestConstants.PUBLISHER_COUNT, bookshelfPublisherRepository.findAll().size());
        bookAuthorRepository.delete(bookAuthor);
        assertEquals(TestConstants.BOOKAUTHOR_COUNT, bookAuthorRepository.findAll().size());
        authorRepository.delete(author);
        assertEquals(TestConstants.AUTHOR_COUNT, authorRepository.findAll().size());
        bookRepository.delete(book);
        assertEquals(TestConstants.BOOK_COUNT, bookRepository.findAll().size());
    }
}
