"""Tests for overlay path generation and directory creation."""
from pathlib import Path

import pytest

from l4d2web.models import Overlay
from l4d2web.services import overlay_creation


def test_generate_overlay_path_returns_str_id() -> None:
    assert overlay_creation.generate_overlay_path(42) == "42"


def test_generate_overlay_path_validates_through_overlay_ref(monkeypatch) -> None:
    # Sanity: numeric paths pass validate_overlay_ref. Anything bizarre would raise.
    assert overlay_creation.generate_overlay_path(1) == "1"


def test_create_overlay_directory_makes_subtree(monkeypatch, tmp_path: Path) -> None:
    monkeypatch.setenv("LEFT4ME_ROOT", str(tmp_path))
    overlay = Overlay(id=7, name="test", path="7", type="workshop", user_id=None)
    overlay_creation.create_overlay_directory(overlay)
    expected = tmp_path / "overlays" / "7"
    assert expected.is_dir()


def test_create_overlay_directory_raises_if_already_exists(
    monkeypatch, tmp_path: Path
) -> None:
    monkeypatch.setenv("LEFT4ME_ROOT", str(tmp_path))
    overlay = Overlay(id=7, name="test", path="7", type="workshop", user_id=None)
    (tmp_path / "overlays" / "7").mkdir(parents=True)
    # exist_ok=False guards against a stray directory shadowing fresh content.
    with pytest.raises(FileExistsError):
        overlay_creation.create_overlay_directory(overlay)
