"""Tests for textual-everforest config helpers."""

from __future__ import annotations

from pathlib import Path

import pytest

from textual_everforest.config import (
    CONFIG_FILENAME,
    apply_theme_from_config,
    default_config_path,
    load_theme_config,
    resolve_theme_name,
    write_theme_config,
)


def test_resolve_theme_name_accepts_shorthand() -> None:
    assert resolve_theme_name("dark") == "everforest-dark-hard"
    assert resolve_theme_name("light") == "everforest-light-hard"
    assert resolve_theme_name("dark-soft") == "everforest-dark-soft"
    assert resolve_theme_name("everforest-light-medium") == "everforest-light-medium"


def test_resolve_theme_name_rejects_unknown() -> None:
    with pytest.raises(ValueError):
        resolve_theme_name("everforest-neon")


def test_write_and_load_theme_config(tmp_path: Path) -> None:
    path = tmp_path / CONFIG_FILENAME
    config = write_theme_config(path, "dark-medium")
    assert config.default_theme == "everforest-dark-medium"

    loaded = load_theme_config(path)
    assert loaded == config


def test_default_config_path_uses_dir(tmp_path: Path) -> None:
    path = default_config_path(tmp_path)
    assert path == tmp_path / CONFIG_FILENAME


def test_apply_theme_from_config(tmp_path: Path) -> None:
    class DummyApp:
        theme = ""

    path = tmp_path / CONFIG_FILENAME
    write_theme_config(path, "light-soft")

    app = DummyApp()
    applied = apply_theme_from_config(app, path)
    assert applied is True
    assert app.theme == "everforest-light-soft"


def test_apply_theme_from_config_missing(tmp_path: Path) -> None:
    class DummyApp:
        theme = ""

    app = DummyApp()
    missing = tmp_path / CONFIG_FILENAME
    applied = apply_theme_from_config(app, missing)
    assert applied is False

    with pytest.raises(FileNotFoundError):
        apply_theme_from_config(app, missing, strict=True)
