package de.thpeetz.kontor.tysc.repository;

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

import java.util.List;

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

import de.thpeetz.kontor.tysc.TestConstants;

@SpringBootTest
class FieldPositionRepositoryTest {

    @Autowired
    private FieldPositionRepository fieldPositionRepository;

    @Autowired
    private SportRepository sportRepository;

    @Test
    void testFindByShortName() {
        FieldPosition position = fieldPositionRepository.findByShortName(TestConstants.QUARTERBACK_SHORTNAME);
        assertNotNull(position);
        assertEquals(TestConstants.QUARTERBACK_NAME, position.getName());
    }

    @Test
    void testFindByShortNameAndSport() {
        Sport sport = sportRepository.findByName(TestConstants.FOOTBALL_NAME);
        FieldPosition position = fieldPositionRepository.findByShortNameAndSport(TestConstants.QUARTERBACK_SHORTNAME, sport);
        assertNotNull(position);
        assertEquals(TestConstants.QUARTERBACK_NAME, position.getName());
    }

    @Test
    void testFindByShortNameIgnoreCase() {
        List<FieldPosition> positions = fieldPositionRepository
                .findByShortNameIgnoreCase(TestConstants.QUARTERBACK_SHORTNAME.toLowerCase());
        assertNotNull(positions);
        assertEquals(1, positions.size());
        assertEquals(TestConstants.QUARTERBACK_NAME, positions.get(0).getName());
    }

    @Test
    void testFindBySport() {
        Sport sport = sportRepository.findByName(TestConstants.FOOTBALL_NAME);
        List<FieldPosition> positions = fieldPositionRepository.findBySport(sport);
        assertEquals(TestConstants.FOOTBALL_POSITION_COUNT, positions.size());
    }

    @Test
    void testSearch() {
        List<FieldPosition> positions = fieldPositionRepository.search("back");
        assertEquals(8, positions.size());
    }
}
