package cz.muni.fi.pa165.soccermanager.repository;

import cz.muni.fi.pa165.soccermanager.PersistenceApplication;
import cz.muni.fi.pa165.soccermanager.entity.Championship;
import cz.muni.fi.pa165.soccermanager.enums.Nation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.annotation.Rollback;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.TestExecutionListeners;
import org.springframework.test.context.testng.AbstractTestNGSpringContextTests;
import org.springframework.test.context.transaction.TransactionalTestExecutionListener;
import org.testng.annotations.Test;

import javax.transaction.Transactional;
import java.util.Collections;
import java.util.List;

import static org.testng.AssertJUnit.assertEquals;
import static org.testng.AssertJUnit.assertTrue;

/**
 * @author Vitalii Bortsov (uco: 514699)
 */
@Transactional
@ContextConfiguration(classes = PersistenceApplication.class)
@TestExecutionListeners(TransactionalTestExecutionListener.class)
public class ChampionshipRepositoryTest extends AbstractTestNGSpringContextTests {

    @Autowired
    private ChampionshipRepository championshipRepository;

    @Rollback
    @Test
    public void createTest() {
        final var c1 = new Championship();
        c1.setName("Moon Championship");
        c1.setCountry(Nation.CZECH_REPUBLIC);
        championshipRepository.save(c1);

        final var res = championshipRepository.findAll();
        assertEquals(res, List.of(c1));
    }

    @Rollback
    @Test
    public void findByIdTest() {
        final var c1 = new Championship();
        c1.setName("Moon Championship");
        c1.setCountry(Nation.CZECH_REPUBLIC);
        championshipRepository.save(c1);

        final var res = championshipRepository.findById(c1.getId());
        assertTrue(res.isPresent());
        assertEquals(res.get(), c1);
    }

    @Rollback
    @Test
    public void removeTest() {
        final var c1 = new Championship();
        c1.setName("Moon Championship");
        c1.setCountry(Nation.CZECH_REPUBLIC);
        championshipRepository.save(c1);

        final var res = championshipRepository.findAll();
        assertEquals(res, List.of(c1));

        championshipRepository.delete(res.get(0));

        final var res2 = championshipRepository.findAll();
        assertEquals(res2, Collections.emptyList());
    }

    @Rollback
    @Test
    public void findAllTest() {

        assertEquals(Collections.emptyList(), championshipRepository.findAll());

        final var c1 = new Championship();
        c1.setName("Moon Championship");
        c1.setCountry(Nation.CZECH_REPUBLIC);
        championshipRepository.save(c1);

        assertEquals(List.of(c1), championshipRepository.findAll());

        final var c2 = new Championship();
        c2.setName("Mars Championship");
        c2.setCountry(Nation.CZECH_REPUBLIC);
        championshipRepository.save(c2);

        assertEquals(List.of(c1, c2), championshipRepository.findAll());

        final var c3 = new Championship();
        c3.setName("Earth Championship");
        c3.setCountry(Nation.CZECH_REPUBLIC);
        championshipRepository.save(c3);

        assertEquals(List.of(c1,c2,c3), championshipRepository.findAll());
    }

    @Rollback
    @Test()
    public void updateTest() {
        final var c1 = new Championship();
        c1.setName("Moon Championship");
        c1.setCountry(Nation.CZECH_REPUBLIC);
        championshipRepository.save(c1);
        c1.setName("Mars championship");

        Championship finded = championshipRepository.findById(c1.getId()).get();
        assertEquals(finded.getName(), c1.getName());
    }
}