"""Tests for tag-based frontmatter query."""

from pathlib import Path

import pytest

from supermemory.init import init_project
from supermemory.query import parse_frontmatter, query, format_results


@pytest.fixture
def memory_with_docs(tmp_path):
    """Create .memory with tagged documents."""
    root = init_project(project="test", base=tmp_path)

    # System doc
    (root / "systems" / "auth.md").write_text(
        "---\nowner: alice\nlast_verified: 2026-01-01\npaths: []\ntags:\n  - auth\n---\n# Auth\n"
    )

    # Gotcha tagged with auth and race-condition
    (root / "gotchas" / "auth-race.md").write_text(
        "---\nseverity: high\nsystems:\n  - auth\ntags:\n  - race-condition\ndiscovered: 2026-01-15\n---\n# Auth Race\n"
    )

    # Gotcha tagged with billing
    (root / "gotchas" / "billing-timeout.md").write_text(
        "---\nseverity: medium\nsystems:\n  - billing\ntags:\n  - performance\ndiscovered: 2026-01-20\n---\n# Billing Timeout\n"
    )

    # Research tagged with auth and security
    (root / "research" / "oauth-flow.md").write_text(
        "---\ndate: 2026-01-10\nauthor: bob\nstatus: current\nsystems:\n  - auth\ntags:\n  - security\n---\n# OAuth Flow\n"
    )

    return root


def test_parse_frontmatter(tmp_path):
    f = tmp_path / "test.md"
    f.write_text("---\ntitle: Hello\ntags:\n  - foo\n---\n# Content\n")

    fm = parse_frontmatter(f)
    assert fm == {"title": "Hello", "tags": ["foo"]}


def test_parse_frontmatter_no_frontmatter(tmp_path):
    f = tmp_path / "test.md"
    f.write_text("# Just a heading\n")

    assert parse_frontmatter(f) is None


def test_parse_frontmatter_empty(tmp_path):
    f = tmp_path / "test.md"
    f.write_text("---\n---\n# Empty frontmatter\n")

    assert parse_frontmatter(f) == {}


def test_query_by_system(memory_with_docs):
    results = query(system="auth", root=memory_with_docs)

    paths = [r["path"] for r in results]
    assert "systems/auth.md" in paths
    assert "gotchas/auth-race.md" in paths
    assert "research/oauth-flow.md" in paths
    assert "gotchas/billing-timeout.md" not in paths


def test_query_by_concern(memory_with_docs):
    results = query(concern="race-condition", root=memory_with_docs)

    paths = [r["path"] for r in results]
    assert "gotchas/auth-race.md" in paths
    assert len(paths) == 1


def test_query_intersection(memory_with_docs):
    results = query(system="auth", concern="security", root=memory_with_docs)

    paths = [r["path"] for r in results]
    assert "research/oauth-flow.md" in paths
    assert len(paths) == 1


def test_query_no_match(memory_with_docs):
    results = query(system="nonexistent", root=memory_with_docs)
    assert results == []


def test_format_results_empty():
    assert "No matching" in format_results([])


def test_format_results_with_data():
    results = [
        {"path": "gotchas/test.md", "frontmatter": {"tags": ["foo"]}, "type": "gotchas"},
    ]
    output = format_results(results)
    assert "1 matching" in output
    assert "gotchas/test.md" in output
