"""Couche logique de l'éditeur de profils (persistance via profile_defaults).

Tests sans display, avec un fichier profiles.yml temporaire.
"""

from __future__ import annotations

from gui_v6.profile_editor import (
    build_profile_spec,
    list_profile_choices,
    profile_is_editable,
    save_profile,
    slug_for_copy,
)


def test_build_profile_spec_structure_and_strip():
    spec = build_profile_spec(
        label="  Mon profil  ",
        description="desc",
        require_manual_mask=True,
        force_disable_vlm=False,
        preferred_manual_mask_template="config/mask_templates/x.json",
        whitelist=["  garder ", "", "garder2"],
        blacklist=["CHUXX"],
        stopwords=[],
    )
    assert spec["label"] == "Mon profil"
    assert spec["require_manual_mask"] is True
    assert spec["force_disable_vlm"] is False
    assert spec["preferred_manual_mask_template"] == "config/mask_templates/x.json"
    assert spec["param_lists"]["whitelist_phrases"] == ["garder", "garder2"]  # strip + vides retirés
    assert spec["param_lists"]["blacklist_force_mask_terms"] == ["CHUXX"]
    assert spec["param_lists"]["additional_stopwords"] == []


def test_save_and_reload_roundtrip(tmp_path):
    profiles = tmp_path / "profiles.yml"
    spec = build_profile_spec(
        label="Test runtime", description="d", require_manual_mask=True,
        force_disable_vlm=True, preferred_manual_mask_template="",
        whitelist=["a"], blacklist=["b", "c"], stopwords=["d"],
    )
    save_profile("mon_profil", spec, path=profiles)

    from profile_defaults import list_effective_profiles
    effective = list_effective_profiles(profiles)
    assert "mon_profil" in effective
    saved = effective["mon_profil"]
    assert saved["label"] == "Test runtime"
    assert saved["require_manual_mask"] is True
    assert saved["force_disable_vlm"] is True
    assert saved["param_lists"]["blacklist_force_mask_terms"] == ["b", "c"]


def test_profile_is_editable_runtime_vs_default(tmp_path):
    profiles = tmp_path / "profiles.yml"
    save_profile("runtime_one", build_profile_spec(label="R1"), path=profiles)
    assert profile_is_editable("runtime_one", path=profiles) is True
    # un profil par défaut (non présent dans l'overlay runtime) n'est pas éditable
    assert profile_is_editable("standard_local", path=profiles) is False


def test_list_profile_choices_marks_editable(tmp_path):
    profiles = tmp_path / "profiles.yml"
    save_profile("runtime_one", build_profile_spec(label="R1"), path=profiles)
    choices = list_profile_choices(path=profiles)
    by_key = {c["key"]: c for c in choices}
    assert by_key["runtime_one"]["editable"] is True
    assert by_key["runtime_one"]["label"] == "R1"
    # un profil défaut présent et non éditable
    assert "standard_local" in by_key
    assert by_key["standard_local"]["editable"] is False


def test_slug_for_copy_avoids_collision():
    assert slug_for_copy("std", set()) == "std_copie"
    assert slug_for_copy("std", {"std_copie"}) == "std_copie_2"
    assert slug_for_copy("std", {"std_copie", "std_copie_2"}) == "std_copie_3"
