import pytest
from pathlib import Path
from src import paths

@pytest.fixture(autouse=True)
def restore_paths():
 # v3 paths.py: PathsConfig is frozen at init time. Save the pre-test
 # config path so we can restore after this test (other tests rely on
 # the conftest-initialized workspace). The fixtures themselves call
 # paths.initialize_paths(<tmp_config>) to control resolution.
 pre = paths.get_config_path()
 yield
 paths.initialize_paths(pre)

def test_default_paths(tmp_path):
 # v3 paths.py: when config has no [paths] section, paths come from
 # defaults. Pass a config that does NOT define [paths].
 root_dir = Path(paths.__file__).resolve().parent.parent
 empty_config = tmp_path / "empty.toml"
 empty_config.write_text("# no [paths] section here\n")
 paths.initialize_paths(empty_config)
 
 assert paths.get_logs_dir() == root_dir / "logs" / "sessions"
 assert paths.get_scripts_dir() == root_dir / "scripts" / "generated"
 assert paths.get_config_path() == empty_config.resolve()

def test_env_var_overrides(tmp_path, monkeypatch):
 # v3 paths.py: env var wins over config and default. Set env var, then
 # init with a config that does NOT define [paths] (so only env + default apply).
 abs_logs = (tmp_path / "abs_logs").resolve()
 monkeypatch.setenv("SLOP_LOGS_DIR", str(abs_logs))
 empty_config = tmp_path / "empty.toml"
 empty_config.write_text("# no [paths] section\n")
 paths.initialize_paths(empty_config)
 assert paths.get_logs_dir() == abs_logs

def test_config_overrides(tmp_path):
 # v3 paths.py: [paths] section in config overrides default. Relative
 # paths in config are resolved against project root.
 root_dir = Path(paths.__file__).resolve().parent.parent
 config_file = tmp_path / "custom_config.toml"
 content = """
[paths]
logs_dir = "cfg_logs"
scripts_dir = "cfg_scripts"
"""
 config_file.write_text(content)
 paths.initialize_paths(config_file)
 
 assert paths.get_logs_dir() == root_dir / "cfg_logs"
 assert paths.get_scripts_dir() == root_dir / "cfg_scripts"

def test_precedence(tmp_path, monkeypatch):
 # v3 paths.py: env var SLOP_LOGS_DIR wins over [paths] config entry.
 root_dir = Path(paths.__file__).resolve().parent.parent
 config_file = tmp_path / "custom_config.toml"
 content = """
[paths]
logs_dir = "cfg_logs"
"""
 config_file.write_text(content)
 # Use absolute env_logs path so _resolve_path returns it as-is.
 env_logs = (root_dir / "env_logs").resolve()
 monkeypatch.setenv("SLOP_LOGS_DIR", str(env_logs))
 paths.initialize_paths(config_file)
 
 # Env var should take precedence over config
 assert paths.get_logs_dir() == env_logs

def test_conductor_dir_project_relative(tmp_path):
 # Should default to tmp_path/conductor
 project_path = str(tmp_path)
 base = (tmp_path / 'conductor').resolve()
 assert paths.get_conductor_dir(project_path) == base
 assert paths.get_tracks_dir(project_path) == base / "tracks"
 assert paths.get_archive_dir(project_path) == base / "archive"
 assert paths.get_track_state_dir("test_track", project_path) == base / "tracks" / "test_track"
