"""Tests for check-conflict hook."""

from pathlib import Path

import pytest

from supermemory.hooks import check_conflict, _parse_do_not_touch
from supermemory.init import init_project


@pytest.fixture
def memory_with_people(tmp_path):
    """Create .memory with people who have DO NOT TOUCH files."""
    root = init_project(project="test", me="alice", base=tmp_path)

    # Bob has claimed some files
    (root / "people" / "bob.md").write_text(
        "---\nid: bob\nrole: engineer\nareas: []\n---\n"
        "# bob\n\n"
        "## Current Focus\n\nWorking on auth.\n\n"
        "## Last Session\n\n"
        "## Owns\n\n"
        "## Working On (DO NOT TOUCH)\n\n"
        "- `src/auth/login.py` — refactoring login flow\n"
        "- `src/auth/middleware.py` — adding rate limiting\n\n"
        "## Preferences\n"
    )

    return root


def test_conflict_detected(memory_with_people):
    result = check_conflict("src/auth/login.py", root=memory_with_people)
    assert result is not None
    assert "bob" in result
    assert "WARNING" in result


def test_no_conflict_different_file(memory_with_people):
    result = check_conflict("src/billing/charge.py", root=memory_with_people)
    assert result is None


def test_no_conflict_own_files(memory_with_people):
    # Alice checking her own files — bob's claims shouldn't apply to alice
    # but alice's person file has the default template content, not real claims
    result = check_conflict("src/utils/helpers.py", root=memory_with_people)
    assert result is None


def test_parse_do_not_touch(tmp_path):
    f = tmp_path / "person.md"
    f.write_text(
        "# bob\n\n"
        "## Working On (DO NOT TOUCH)\n\n"
        "- `src/foo.py` — reason\n"
        "- `src/bar.py` — other reason\n\n"
        "## Preferences\n"
    )
    paths = _parse_do_not_touch(f)
    assert paths == ["src/foo.py", "src/bar.py"]


def test_parse_do_not_touch_empty(tmp_path):
    f = tmp_path / "person.md"
    f.write_text("# bob\n\n## Working On (DO NOT TOUCH)\n\n## Preferences\n")
    paths = _parse_do_not_touch(f)
    assert paths == []
