from pathlib import Path
import pytest
from src.theme_models import ThemeFile, ThemePalette, load_theme_file


FIXTURES = Path(__file__).parent / "fixtures" / "themes"


def test_load_minimal_theme_file():
    p = FIXTURES / "minimal.toml"
    theme = load_theme_file(p, scope="project")
    assert theme.syntax_palette == "dark"
    assert theme.palette.window_bg == (10, 20, 30)
    assert theme.palette.text == (200, 200, 200)
    assert theme.palette.button_hovered == (255, 100, 50)
    assert theme.source_path == p
    assert theme.scope == "project"


def test_missing_required_keys_raises():
    p = FIXTURES / "missing_required.toml"
    with pytest.raises(ValueError, match=r"missing required \[colors\]"):
        load_theme_file(p, scope="project")


def test_invalid_syntax_palette_raises():
    p = FIXTURES / "minimal.toml"
    with pytest.raises(ValueError, match=r"invalid syntax_palette"):
        ThemeFile(name="x", palette=ThemePalette(), syntax_palette="not_a_real_palette", source_path=p, scope="project")


def test_round_trip_to_from_dict():
    p = FIXTURES / "minimal.toml"
    loaded = load_theme_file(p, scope="project")
    dumped = loaded.to_dict()
    reloaded = ThemeFile.from_dict(loaded.name, dumped, source_path=p, scope="project")
    assert reloaded.syntax_palette == loaded.syntax_palette
    assert reloaded.palette.window_bg == loaded.palette.window_bg
    assert reloaded.palette.text == loaded.palette.text


def test_scope_setter():
    p = FIXTURES / "minimal.toml"
    theme = load_theme_file(p, scope="global")
    assert theme.scope == "global"
    themed_as_project = theme.with_scope("project")
    assert themed_as_project.scope == "project"
    assert themed_as_project.name == theme.name
