package cz.moneta.test.sandbox.generators;

import cz.moneta.test.dsl.Harness;
import cz.moneta.test.dsl.util.data.client.IdCard;
import cz.moneta.test.dsl.util.data.client.NonClient;
import cz.moneta.test.harness.annotations.TestCase;
import cz.moneta.test.harness.annotations.TestScenario;
import org.junit.jupiter.api.Assertions;

import java.time.LocalDate;
import java.time.ZoneId;

@TestScenario(name = "check ID card validity")
public class IdCardGeneratorTest {

    @TestCase(name = "under 15")
    public void check_valid_before_15(Harness harness) {
        NonClient nonClient = harness.data().newNonClient();
        IdCard idCard = nonClient.idCard(LocalDate.of(2016, 1, 1));
        Assertions.assertEquals(idCard.getValidFrom().plusYears(5), idCard.getValidTo());
        Assertions.assertEquals(
                idCard.getValidFrom().plusYears(5),
                nonClient.idCardExpiredRaw().toInstant().atZone(ZoneId.systemDefault()).toLocalDate());
        Assertions.assertTrue(nonClient.birthNumber().startsWith("16"));
    }

    @TestCase(name = "test over 70")
    public void check_validity_after_70(Harness harness) {
        NonClient nonClient = harness.data().newNonClient();
        IdCard idCard = nonClient.idCard(LocalDate.of(1915, 1, 1));
        Assertions.assertEquals(idCard.getValidFrom().plusYears(35), idCard.getValidTo());
        Assertions.assertEquals(
                idCard.getValidFrom().plusYears(35),
                nonClient.idCardExpiredRaw().toInstant().atZone(ZoneId.systemDefault()).toLocalDate());
        Assertions.assertTrue(nonClient.birthNumber().startsWith("15"));
    }

    @TestCase(name = "test default validity")
    public void check_default_validity(Harness harness) {
        NonClient nonClient = harness.data().newNonClient();
        IdCard idCard = nonClient.idCard(LocalDate.of(1996, 1, 1));
        Assertions.assertEquals(idCard.getValidFrom().plusYears(10), idCard.getValidTo());
        Assertions.assertEquals(
                idCard.getValidFrom().plusYears(10),
                nonClient.idCardExpiredRaw().toInstant().atZone(ZoneId.systemDefault()).toLocalDate());
        Assertions.assertTrue(nonClient.birthNumber().startsWith("96"));
    }

    @TestCase(name = "test pre-defined RC < 15 years old")
    public void validity_with_custom_rc(Harness harness) {
        NonClient nonClient = harness.data().newNonClient();
        nonClient.withBirthNumber("0912316482");
        IdCard idCard = nonClient.idCard();
        Assertions.assertEquals(idCard.getValidFrom().plusYears(5), idCard.getValidTo());
        Assertions.assertTrue(nonClient.birthNumber().startsWith("09"));
    }

    @TestCase(name = "test random card validity")
    public void check_random_validity(Harness harness) {
        NonClient nonClient = harness.data().newNonClient();
        IdCard idCard = nonClient.idCard();
        Assertions.assertTrue(idCard.getValidTo().isAfter(LocalDate.now()));
        Assertions.assertTrue(idCard.getValidTo().isBefore(LocalDate.now().plusYears(10)));
    }
}

