
from pathlib import Path

from noveler.infrastructure.adapters.path_service_adapter import (
    PathServiceAdapter,
    create_path_service,
)


def test_get_step_output_file_path_uses_session_format(tmp_path):
    adapter = PathServiceAdapter(tmp_path)
    path = adapter.get_step_output_file_path(
        episode_number=2,
        step_number=7,
        timestamp="20251004120030",
    )
    # Current implementation: EP{episode:04d}_step{step:02d}_{timestamp}.json
    # Stored in .noveler/checks/ directory
    expected = tmp_path / ".noveler" / "checks" / "EP0002_step07_20251004120030.json"
    assert path == expected


def test_get_step_output_file_path_uses_default_timestamp_when_omitted(tmp_path):
    # Test that when timestamp is omitted, a default timestamp is generated
    adapter = PathServiceAdapter(tmp_path)
    path = adapter.get_step_output_file_path(
        episode_number=1,
        step_number=3,
    )
    # File should exist and have the standard naming pattern
    assert path.name.startswith("EP0001_step03_")
    assert path.name.endswith(".json")


def test_get_step_output_file_path_falls_back_without_session(tmp_path):
    adapter = PathServiceAdapter(tmp_path)
    path = adapter.get_step_output_file_path(episode_number=4, step_number=5, timestamp="20251004100533")
    assert path.name == "EP0004_step05_20251004100533.json"


def test_get_write_step_output_file_path_uses_standard_format(tmp_path):
    adapter = PathServiceAdapter(tmp_path)
    path = adapter.get_write_step_output_file_path(
        episode_number=3,
        step_number=9,
        timestamp="20251004141500",
    )
    # Uses same format as get_step_output_file_path but in .noveler/writes/ directory
    expected = tmp_path / ".noveler" / "writes" / "EP0003_step09_20251004141500.json"
    assert path == expected


def test_create_path_service_enables_strict_mode_from_env(monkeypatch, tmp_path):
    # Test that NOVELER_STRICT_PATHS env var enables strict mode
    monkeypatch.setenv("NOVELER_STRICT_PATHS", "true")

    captured: dict[str, object] = {}

    class DummyAdapter:
        def __init__(self, project_root, config_repository=None, strict=None):
            captured["project_root"] = project_root
            captured["strict"] = strict

    monkeypatch.setattr(
        "noveler.infrastructure.adapters.path_service_adapter.PathServiceAdapter",
        DummyAdapter,
    )

    create_path_service(tmp_path)

    assert captured["project_root"] == tmp_path
    assert captured["strict"] is True


def test_create_path_service_auto_detects_root_when_not_provided(monkeypatch):
    # Test that when project_root is None, auto-detection is used
    mock_root = Path("/detected/root")
    monkeypatch.setattr(
        "noveler.infrastructure.adapters.path_service_adapter.detect_project_root",
        lambda: mock_root,
    )

    captured: dict[str, object] = {}

    class DummyAdapter:
        def __init__(self, project_root, config_repository=None, strict=None):
            captured["project_root"] = project_root
            captured["strict"] = strict

    monkeypatch.setattr(
        "noveler.infrastructure.adapters.path_service_adapter.PathServiceAdapter",
        DummyAdapter,
    )

    create_path_service(project_root=None)
    assert captured["project_root"] == mock_root
