from pathlib import Path

from app.config import Settings
from app.docs_repository import DocsRepository
from app.freshness import build_freshness_report
from app.validation import validate_repository


def test_lists_docs_from_repo_root() -> None:
    repo_root = Path(__file__).resolve().parents[2]
    docs = DocsRepository(Settings(repo_root)).list_docs()

    paths = {doc["path"] for doc in docs}
    assert "10-systems/hardware/hp-proliant-dl380-g6.md" in paths
    assert "90-maintenance/documentation-rules.md" in paths


def test_reads_frontmatter() -> None:
    repo_root = Path(__file__).resolve().parents[2]
    doc = DocsRepository(Settings(repo_root)).read_doc("10-systems/hardware/hp-proliant-dl380-g6.md")

    assert doc["frontmatter"]["owner"] == "gmikcon"
    assert "KVM/QEMU" in doc["body"]


def test_freshness_report_has_no_missing_doc_links() -> None:
    repo_root = Path(__file__).resolve().parents[2]
    report = build_freshness_report(Settings(repo_root))

    missing_link_issues = [
        issue for issue in report["issues"] if issue["code"] == "missing-doc-link-target"
    ]
    assert missing_link_issues == []


def test_freshness_report_is_clean_for_knowledge_docs() -> None:
    repo_root = Path(__file__).resolve().parents[2]
    report = build_freshness_report(Settings(repo_root))

    assert report["status"] == "ok"
    assert report["issues"] == []


def test_validation_report_is_clean() -> None:
    repo_root = Path(__file__).resolve().parents[2]
    report = validate_repository(Settings(repo_root))

    assert report["status"] == "ok"
    assert report["issues"] == []
