"""Tests for prompt_config.py — config loader for prompt constants and lexicon."""
import re
import pytest


def test_load_constants_returns_dict():
    from recoil.core.prompt_config import load_constants
    c = load_constants()
    assert isinstance(c, dict)
    assert "production" in c
    assert "casting" in c
    assert "shared" in c


def test_get_production_constant():
    from recoil.core.prompt_config import get_constant
    val = get_constant("production", "camera_body")
    assert val == "Arri Alexa Mini LF"


def test_get_casting_constant():
    from recoil.core.prompt_config import get_constant
    val = get_constant("casting", "casting_camera")
    assert "Alexa 65" in val


def test_get_constant_with_project_override(tmp_path):
    """Project-level prompt_constants.json overrides global."""
    import json
    from recoil.core.prompt_config import get_constant
    override = {"production": {"camera_body": "RED V-Raptor"}}
    override_path = tmp_path / "prompt_constants.json"
    override_path.write_text(json.dumps(override))
    val = get_constant("production", "camera_body", project_dir=str(tmp_path))
    assert val == "RED V-Raptor"


def test_get_constant_missing_key_returns_default():
    from recoil.core.prompt_config import get_constant
    val = get_constant("production", "nonexistent_key", default="fallback")
    assert val == "fallback"


def test_load_lexicon_returns_kinetic_map():
    from recoil.core.prompt_config import load_lexicon
    lex = load_lexicon()
    assert "kinetic_map" in lex
    assert len(lex["kinetic_map"]) == 14
    assert "fallback" in lex


def test_get_kinetic_descriptor_matches():
    from recoil.core.prompt_config import get_kinetic_descriptor
    desc = get_kinetic_descriptor("She pushes the door open")
    assert "muscles taut" in desc


def test_get_kinetic_descriptor_fallback():
    from recoil.core.prompt_config import get_kinetic_descriptor
    desc = get_kinetic_descriptor("The table is brown")
    assert desc == "natural posture, documentary framing, ambient atmosphere"


def test_get_all_kinetic_descriptors_matches():
    from recoil.core.prompt_config import get_all_kinetic_descriptors
    descs = get_all_kinetic_descriptors("She pushes the door open")
    assert isinstance(descs, list)
    assert len(descs) >= 1
    assert any("muscles taut" in d for d in descs)


def test_get_all_kinetic_descriptors_max_count():
    from recoil.core.prompt_config import get_all_kinetic_descriptors
    descs = get_all_kinetic_descriptors("She pushes and runs and fights", max_count=2)
    assert len(descs) <= 2


def test_get_all_kinetic_descriptors_fallback():
    from recoil.core.prompt_config import get_all_kinetic_descriptors, get_constant
    descs = get_all_kinetic_descriptors("The table is brown")
    assert isinstance(descs, list)
    assert len(descs) == 1
    assert descs[0] == get_constant("shared", "kinetic_fallback")


def test_get_all_kinetic_descriptors_dedup():
    """Duplicate descriptors are not returned."""
    from recoil.core.prompt_config import get_all_kinetic_descriptors
    # Even if multiple patterns match for the same descriptor, list should be unique
    descs = get_all_kinetic_descriptors("She pushes the door open")
    assert len(descs) == len(set(descs))


def test_get_lighting_direction():
    from recoil.core.prompt_config import get_lighting_direction
    d = get_lighting_direction("overhead fluorescent strips")
    assert d == "TOP"


def test_get_lighting_direction_unknown():
    from recoil.core.prompt_config import get_lighting_direction
    d = get_lighting_direction("some random lighting")
    assert d is None


def test_get_lighting_quality():
    from recoil.core.prompt_config import get_lighting_quality
    q = get_lighting_quality("harsh directional beam")
    assert q == "hard"


def test_get_lighting_quality_unknown():
    from recoil.core.prompt_config import get_lighting_quality
    q = get_lighting_quality("ordinary room light")
    assert q is None
