package com.astalange.core;

import com.astalange.core.entity.*;
import org.junit.jupiter.api.Test;

import java.time.LocalTime;

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

public class CreneauEntrainementTest {

    @Test
    public void testStartAndEndColumns() {
        CreneauEntrainement c1 = new CreneauEntrainement();
        c1.setHeureDebut(LocalTime.of(8, 0));
        c1.setHeureFin(LocalTime.of(9, 30));

        // Start time 08:00 is (8 * 60) = 480 mins. Offset from 08:00 is 0 mins.
        // Column is (0 / 15) + 3 = 3.
        assertEquals(3, c1.getStartColumn());

        // End time 09:30 is (9*60 + 30) = 570 mins. Offset from 08:00 is 90 mins.
        // Column is (90 / 15) + 3 = 9.
        assertEquals(9, c1.getEndColumn());
    }

    @Test
    public void testStartAndEndRows() {
        CreneauEntrainement c1 = new CreneauEntrainement();
        
        // Test terrain complet (baseRow index 0 is 3, index 1 is 8)
        c1.setZone(ZoneTerrain.COMPLET);
        assertEquals(3, c1.getStartRow(0));
        assertEquals(7, c1.getEndRow(0));
        
        assertEquals(8, c1.getStartRow(1));
        assertEquals(12, c1.getEndRow(1));

        // Test demi-terrain A (Quarts 1 and 2)
        c1.setZone(ZoneTerrain.DEMI_A);
        assertEquals(3, c1.getStartRow(0));
        assertEquals(5, c1.getEndRow(0));

        // Test demi-terrain B (Quarts 3 and 4)
        c1.setZone(ZoneTerrain.DEMI_B);
        assertEquals(5, c1.getStartRow(0));
        assertEquals(7, c1.getEndRow(0));
        
        // Test single Quarts
        c1.setZone(ZoneTerrain.QUART_1);
        assertEquals(3, c1.getStartRow(0));
        assertEquals(4, c1.getEndRow(0));

        c1.setZone(ZoneTerrain.QUART_4);
        assertEquals(6, c1.getStartRow(0));
        assertEquals(7, c1.getEndRow(0));
    }

    @Test
    public void testOverlapsWith() {
        Terrain t1 = new Terrain();
        t1.setId(1L);
        Terrain t2 = new Terrain();
        t2.setId(2L);

        CreneauEntrainement c1 = new CreneauEntrainement();
        c1.setJourSemaine(JourSemaine.LUNDI);
        c1.setTerrain(t1);
        c1.setZone(ZoneTerrain.DEMI_A);
        c1.setHeureDebut(LocalTime.of(18, 0));
        c1.setHeureFin(LocalTime.of(19, 30));

        // 1. Different day -> No overlap
        CreneauEntrainement c2 = new CreneauEntrainement();
        c2.setJourSemaine(JourSemaine.MARDI);
        c2.setTerrain(t1);
        c2.setZone(ZoneTerrain.DEMI_A);
        c2.setHeureDebut(LocalTime.of(18, 0));
        c2.setHeureFin(LocalTime.of(19, 30));
        assertFalse(c1.overlapsWith(c2));

        // 2. Different terrain -> No overlap
        c2.setJourSemaine(JourSemaine.LUNDI);
        c2.setTerrain(t2);
        assertFalse(c1.overlapsWith(c2));

        // 3. Same day, same terrain, non-overlapping times -> No overlap
        c2.setTerrain(t1);
        c2.setHeureDebut(LocalTime.of(19, 30));
        c2.setHeureFin(LocalTime.of(21, 0));
        assertFalse(c1.overlapsWith(c2));

        // 4. Same day, same terrain, overlapping times, non-overlapping zones (Demi_A and Demi_B don't overlap)
        c2.setHeureDebut(LocalTime.of(18, 30));
        c2.setHeureFin(LocalTime.of(20, 0));
        c2.setZone(ZoneTerrain.DEMI_B);
        assertFalse(c1.overlapsWith(c2));

        // 5. Same day, same terrain, overlapping times, overlapping zones (Demi_A and Complet overlap!)
        c2.setZone(ZoneTerrain.COMPLET);
        assertTrue(c1.overlapsWith(c2));

        // 6. Same day, same terrain, overlapping times, overlapping zones (Demi_A and Quart_1 overlap!)
        c2.setZone(ZoneTerrain.QUART_1);
        assertTrue(c1.overlapsWith(c2));
    }
}
