#!/usr/bin/env python3
from pathlib import Path

from profile_defaults import (
    delete_runtime_profile,
    ensure_runtime_profiles_config,
    get_default_profile_key,
    list_default_profile_keys,
    list_effective_profiles,
    load_effective_profiles_dict,
    read_default_profiles_text,
    read_runtime_profiles_overlay_text,
    save_runtime_profile,
    set_runtime_default_profile,
)


def test_default_profiles_template_is_externalized():
    text = read_default_profiles_text()

    assert "default_profile:" in text
    assert "chuxx_strict:" in text
    assert "partage_recherche:" in text
    assert "standard_local" in list_default_profile_keys()


def test_runtime_profiles_overlay_template_is_minimal():
    text = read_runtime_profiles_overlay_text()

    assert "profiles.default.yml" in text
    assert "{}" in text


def test_runtime_profiles_overlay_is_created_and_merged(tmp_path: Path):
    cfg_path = tmp_path / "profiles.yml"

    created = ensure_runtime_profiles_config(cfg_path)
    assert created == cfg_path
    assert cfg_path.exists()

    effective = load_effective_profiles_dict(cfg_path)
    assert effective["default_profile"] == "standard_local"

    cfg_path.write_text(
        "default_profile: partage_recherche\n"
        "profiles:\n"
        "  partage_recherche:\n"
        "    description: Profil local surcharge\n",
        encoding="utf-8",
    )

    effective = load_effective_profiles_dict(cfg_path)
    assert effective["default_profile"] == "partage_recherche"
    assert effective["profiles"]["partage_recherche"]["description"] == "Profil local surcharge"


def test_list_effective_profiles_normalizes_flags(tmp_path: Path):
    cfg_path = tmp_path / "profiles.yml"
    cfg_path.write_text(
        "profiles:\n"
        "  custom:\n"
        "    label: Profil custom\n"
        "    require_manual_mask: true\n"
        "    force_disable_vlm: true\n"
        "    preferred_manual_mask_template: hopital/formulaire.yml\n"
        "    param_lists:\n"
        "      whitelist_phrases:\n"
        "      - DOCUMENT INTERNE\n"
        "      blacklist_force_mask_terms:\n"
        "      - CUSTOM_ETAB\n"
        "      additional_stopwords:\n"
        "      - DIM\n"
        "    dictionaries_overlay:\n"
        "      blacklist:\n"
        "        force_mask_terms:\n"
        "        - CUSTOM_ETAB\n",
        encoding="utf-8",
    )

    profiles = list_effective_profiles(cfg_path)

    assert profiles["custom"]["label"] == "Profil custom"
    assert profiles["custom"]["require_manual_mask"] is True
    assert profiles["custom"]["force_disable_vlm"] is True
    assert profiles["custom"]["preferred_manual_mask_template"] == "hopital/formulaire.yml"
    assert profiles["custom"]["has_param_lists"] is True
    assert profiles["custom"]["param_lists"]["whitelist_phrases"] == ["DOCUMENT INTERNE"]
    assert profiles["custom"]["param_lists"]["blacklist_force_mask_terms"] == ["CUSTOM_ETAB"]
    assert profiles["custom"]["param_lists"]["additional_stopwords"] == ["DIM"]
    assert "CUSTOM_ETAB" in profiles["custom"]["dictionaries_overlay"]["blacklist"]["force_mask_terms"]


def test_default_profile_key_keeps_merged_default_when_available(tmp_path: Path):
    cfg_path = tmp_path / "profiles.yml"
    cfg_path.write_text(
        "default_profile: missing\n"
        "profiles:\n"
        "  custom:\n"
        "    label: Profil custom\n",
        encoding="utf-8",
    )

    assert get_default_profile_key(cfg_path) == "standard_local"


def test_save_runtime_profile_persists_new_profile_and_default(tmp_path: Path):
    cfg_path = tmp_path / "profiles.yml"

    save_runtime_profile(
        "bureau_strict",
        {
            "label": "Bureau strict",
            "description": "Profil créé depuis la GUI",
            "require_manual_mask": True,
            "force_disable_vlm": True,
            "preferred_manual_mask_template": "chuxx/formulaire.yml",
            "has_preferred_manual_mask_template": True,
            "param_lists": {
                "whitelist_phrases": ["VALIDATION DIM"],
                "blacklist_force_mask_terms": ["CHUXX"],
                "additional_stopwords": ["RUM"],
            },
            "has_param_lists": True,
            "dictionaries_overlay": {
                "blacklist": {
                    "force_mask_terms": ["CHUXX"],
                },
            },
        },
        cfg_path,
        set_default=True,
    )

    data = load_effective_profiles_dict(cfg_path)
    assert data["default_profile"] == "bureau_strict"

    saved = list_effective_profiles(cfg_path)["bureau_strict"]
    assert saved["label"] == "Bureau strict"
    assert saved["require_manual_mask"] is True
    assert saved["force_disable_vlm"] is True
    assert saved["preferred_manual_mask_template"] == "chuxx/formulaire.yml"
    assert saved["param_lists"]["whitelist_phrases"] == ["VALIDATION DIM"]
    assert saved["param_lists"]["blacklist_force_mask_terms"] == ["CHUXX"]
    assert saved["param_lists"]["additional_stopwords"] == ["RUM"]


def test_set_and_delete_runtime_profile(tmp_path: Path):
    cfg_path = tmp_path / "profiles.yml"
    save_runtime_profile(
        "profil_temporaire",
        {
            "label": "Profil temporaire",
            "description": "",
            "require_manual_mask": False,
            "force_disable_vlm": False,
            "dictionaries_overlay": {},
        },
        cfg_path,
    )

    set_runtime_default_profile("profil_temporaire", cfg_path)
    assert get_default_profile_key(cfg_path) == "profil_temporaire"

    delete_runtime_profile("profil_temporaire", cfg_path)
    profiles = list_effective_profiles(cfg_path)
    assert "profil_temporaire" not in profiles
    assert get_default_profile_key(cfg_path) == "standard_local"
