"""Formatprüfung CSV-Vorlagen (template_validator)."""

from __future__ import annotations

from csv_parser.template_validator import validate_csv_template


def test_validate_kj_column_warns_without_source_unit():
    r = validate_csv_template(
        "activity",
        {"Aktive Energie (kJ)": "kcal_active", "Start": "start_time", "Trainingsart": "activity_type"},
        {"start_time": {"type": "datetime", "format": "yyyy-mm-dd HH:MM:SS", "flexible": True}},
        None,
        None,
    )
    assert r["valid"] is True
    codes = {w["code"] for w in r["warnings"]}
    assert "energy_kj_without_source_unit" in codes


def test_validate_invalid_target_error():
    r = validate_csv_template(
        "activity",
        {"X": "not_a_field"},
        {},
        None,
        None,
    )
    assert r["valid"] is False
    assert any(e["code"] == "invalid_field_mapping" for e in r["errors"])


def test_signature_normalized_vs_raw_mapping_no_false_warning():
    """Analyse liefert normalisierte Signatur; field_mappings nutzt Original-Header wie im Seed."""
    r = validate_csv_template(
        "blood_pressure",
        {
            "Datum": "measured_date",
            "Zeit": "measured_time",
            "Systolisch (mmHg)": "systolic",
            "Diastolisch (mmHg)": "diastolic",
            "Puls (bpm)": "pulse",
        },
        {
            "measured_date": {"type": "date", "format": "dd.mm.yyyy"},
            "measured_time": {"type": "time", "format": "HH:MM"},
            "systolic": {"type": "int"},
            "diastolic": {"type": "int"},
            "pulse": {"type": "int"},
        },
        None,
        ["datum", "diastolisch_mmhg", "puls_bpm", "systolisch_mmhg", "zeit"],
    )
    assert r["valid"] is True
    assert not any(w["code"] == "signature_vs_mappings_mismatch" for w in r["warnings"])


def test_validate_duplicate_target_warning():
    r = validate_csv_template(
        "weight",
        {"A": "weight", "B": "weight", "Tag": "date"},
        {
            "weight": {"type": "float", "decimal_separator": "."},
            "date": {"type": "date", "format": "yyyy-mm-dd", "flexible": True},
        },
        None,
        None,
    )
    assert r["valid"] is True
    assert any(w["code"] == "duplicate_target_columns" for w in r["warnings"])
