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

import cz.muni.fi.pa165.soccermanager.PersistenceApplication;
import cz.muni.fi.pa165.soccermanager.entity.League;
import cz.muni.fi.pa165.soccermanager.entity.Team;
import org.springframework.beans.factory.annotation.Autowired;
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.BeforeMethod;
import org.testng.annotations.Test;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.transaction.Transactional;
import java.time.LocalDate;
import java.util.List;

import static org.assertj.core.api.Assertions.assertThat;
import static org.testng.AssertJUnit.assertFalse;
import static org.testng.AssertJUnit.assertTrue;

/**
 * Team Dao test
 *
 * @author Petr Balazy (uco: 527362)
 */
@ContextConfiguration(classes = PersistenceApplication.class)
@TestExecutionListeners(TransactionalTestExecutionListener.class)
@Transactional
public class TeamRepositoryTest extends AbstractTestNGSpringContextTests {

    @PersistenceContext
    protected EntityManager em;

    Team team1;
    Team team2;
    League league;

    @Autowired
    private TeamRepository teamRepository;

    @BeforeMethod
    public void setUp() {
        league = new League();
        league.setName("Moon League");
        league.setEndDate(LocalDate.now());
        league.setStartDate(LocalDate.now());

        team1 = new Team();
        team1.setGround("Prima stadion");
        team1.setName("FC Slavkov");
        team1.setLeague(league);
        em.persist(team1);

        team2 = new Team();
        team2.setGround("Pavluv stadion");
        team2.setName("FC Opava");
        team2.setLeague(league);
        em.persist(team2);
        league.setTeams(List.of(team1, team2));
        em.persist(league);
    }

    @Test
    public void createTest() {
        final var team = new Team();
        team.setGround("UP stadion");
        team.setName("FC Hradiste");

        teamRepository.save(team);

        final var createdTeam = em.find(Team.class, team.getId());
        assertThat(createdTeam).isEqualTo(team);
    }

    @Test
    public void findByIdTest() {
        final var t = teamRepository.findById(team1.getId());
        assertTrue(t.isPresent());
        assertThat(t.get()).isEqualTo(team1);

        final var t2 = teamRepository.findById(100L);
        assertFalse(t2.isPresent());
    }

    @Test
    public void findAllTest() {
        final var teams = teamRepository.findAll();
        assertThat(teams).hasSize(2).containsExactly(team1, team2);
    }


    @Test
    public void removeTest() {
        final var teams = teamRepository.findAll();
        assertThat(teams).hasSize(2).containsExactly(team1, team2);

        teamRepository.delete(team2);

        final var teams2 = teamRepository.findAll();
        assertThat(teams2).hasSize(1).containsExactly(team1);
    }

    @Test
    public void findLeagueTest() {
        final var teams = teamRepository.findByLeague(league.getId());
        assertThat(teams).hasSize(2).containsExactly(team1, team2);
    }
}


