:- use_module(library(plunit)).
:- use_module(library(lists)).
:- use_module(library(aggregate)).
:- use_module('../prolog/fr_bareme_km').

:- begin_tests(fr_bareme_km).

% Official example, service-public.gouv.fr A14686 (09/04/2026):
% 4 000 km in 2025 with a 5 CV car -> 2 544 EUR (4 000 x 0,636).
test(service_public_2026_5cv_4000km) :-
    frais_km(2026, automobile, 5, 4000, M),
    M =:= 2544.

% Middle band, 2026 scale: (d x 0,316) + 1 065 for 3 CV and less.
test(auto_2026_3cv_10000km) :-
    frais_km(2026, automobile, 3, 10000, M),
    M =:= 4225.

% BOFIP BOI-BAREME-000001, revenus 2021 (declaration 2022): d x 0,446, 7 CV+.
test(auto_2022_9cv_25000km) :-
    frais_km(2022, automobile, 9, 25000, M),
    M =:= 11150.

% Motorcycle more than 5 CV, 2026: (d x 0,079) + 1 583.
test(moto_2026_8cv_4500km) :-
    frais_km(2026, motocyclette, 8, 4500, M),
    M =:= 1938.5.

% Moped, 2026: d x 0,315 up to 3 000 km.
test(cyclo_2026_2000km) :-
    frais_km(2026, cyclomoteur, 1, 2000, M),
    M =:= 630.

% Electric vehicle: +20 % on the amount (CGI ann. IV art. 6 B, II).
test(electrique_2026_5cv_4000km) :-
    frais_km(2026, automobile, 5, 4000, [electrique(true)], M),
    M =:= 3052.8.

% Band limits: 5 000 km is in the first band, 5 001 in the second.
test(limite_tranche_5000) :-
    frais_km(2026, automobile, 5, 5000, M1),
    frais_km(2026, automobile, 5, 5001, M2),
    M1 =:= 3180,
    M2 =:= 3180.36.

% Scale frozen since the 2023 revaluation (+5,4 %): 2023..2026 identical.
test(gel_2023_2026, [nondet]) :-
    forall(bareme_km(2023, V, C, T, F),
           forall(member(A, [2024, 2025, 2026]), bareme_km(A, V, C, T, F))).

test(nombre_de_faits) :-
    aggregate_all(count, bareme_km(_, _, _, _, _), N),
    N =:= 135.

test(annee_non_couverte, [fail]) :-
    frais_km(2021, automobile, 5, 4000, _).

test(annee_revenus) :-
    annee_revenus(2026, R),
    R =:= 2025.

:- end_tests(fr_bareme_km).
