package com.astalange.core;

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

import java.math.BigDecimal;
import java.time.LocalDate;
import java.util.List;

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

public class AdherentRecalculationTest {

    @Test
    public void testLicencePriceCalculationBasedOnResidencyAndCategory() {
        Adherent adherent = new Adherent();
        adherent.setNom("Doe");
        adherent.setPrenom("John");
        adherent.setDateNaissance(LocalDate.of(2015, 5, 10)); // born 2015
        adherent.setResidentTalange(true);

        Categorie u11 = new Categorie();
        u11.setNom("U11");
        u11.setAnneeMin(2015);
        u11.setAnneeMax(2015);
        u11.setTarifBase(new BigDecimal("130.00"));
        u11.setTarifExterieur(new BigDecimal("160.00"));

        Licence licence = new Licence();
        licence.setAdherent(adherent);
        licence.setCategorie(u11);

        // Under residentTalange = true, base price should be 130
        assertEquals(new BigDecimal("130.00"), licence.getPrixTotal());

        // Under residentTalange = false, base price should be 160
        adherent.setResidentTalange(false);
        assertEquals(new BigDecimal("160.00"), licence.getPrixTotal());
    }

    @Test
    public void testCategoryUpdateOnBirthdateChangeSimulation() {
        // Set up adherent
        Adherent adherent = new Adherent();
        adherent.setDateNaissance(LocalDate.of(2015, 5, 10)); // 2015 birth year

        Saison saison = new Saison();
        saison.setNom("2025-2026");
        saison.setEstActive(true);

        // Set up categories
        Categorie u11 = new Categorie();
        u11.setNom("U11");
        u11.setAnneeMin(2015);
        u11.setAnneeMax(2015);
        u11.setSaison(saison);
        u11.setTarifBase(new BigDecimal("130.00"));

        Categorie u12 = new Categorie();
        u12.setNom("U12");
        u12.setAnneeMin(2014);
        u12.setAnneeMax(2014);
        u12.setSaison(saison);
        u12.setTarifBase(new BigDecimal("140.00"));

        List<Categorie> categories = List.of(u11, u12);

        // Licence initially created for U11
        Licence licence = new Licence();
        licence.setAdherent(adherent);
        licence.setCategorie(u11);
        licence.setSaison(saison);

        // Simulation of date of birth changing to 2014 (U12)
        adherent.setDateNaissance(LocalDate.of(2014, 5, 10));
        int newBirthYear = adherent.getDateNaissance().getYear();

        // Simulate Controller's update logic
        Categorie newCat = categories.stream()
                .filter(c -> newBirthYear >= c.getAnneeMin() && newBirthYear <= c.getAnneeMax())
                .findFirst()
                .orElse(null);

        assertNotNull(newCat);
        assertEquals("U12", newCat.getNom());
        licence.setCategorie(newCat);

        // Verify the licence's category has updated to U12
        assertEquals("U12", licence.getCategorie().getNom());
        // Verify price total is updated dynamically
        assertEquals(new BigDecimal("140.00"), licence.getPrixTotal());
    }
}
