from pathlib import Path

from products.plan_validator import validate_plan


def test_validate_plan_strict_ok(tmp_path: Path):
    plan = tmp_path / "plan.md"
    html = tmp_path / "plan.html"
    plan.write_text(
        """---
product_id: sample-product
purpose: prove plan validator
references:
  - products/AGENT_PRODUCTS.md
---

## Purpose
Prove the validator catches structure.

## Problem
No problem.

## Solution
A minimal plan structure.

## Files
- products/generated/sample-product/spec.json

## Phases
- [ ] Plan
- [ ] Build

## Validation
- pytest

## References
- products/AGENT_PRODUCTS.md

## Notes
Seeded plan.
""",
        encoding="utf-8",
    )
    html.write_text("<section>sample</section>", encoding="utf-8")

    result = validate_plan(plan, strict=True)

    assert result["ok"] is True
    assert result["frontmatter"]["product_id"] == "sample-product"
    assert result["html"] == str(html)
    assert result["errors"] == []


def test_validate_plan_strict_fails_missing_sections(tmp_path: Path):
    plan = tmp_path / "plan.md"
    plan.write_text(
        """---
product_id: sample-product
purpose: bad plan
---

## Purpose
Missing fields.
""",
        encoding="utf-8",
    )

    result = validate_plan(plan, strict=True)

    assert result["ok"] is False
    assert any(err.startswith("missing sections") for err in result["errors"])
    assert any(err.startswith("missing frontmatter") for err in result["errors"])
    assert "references missing or empty" in result["errors"]
    assert "html plan missing" in result["errors"]

    assert result["frontmatter"]["product_id"] == "sample-product"


def test_validate_plan_frontmatter_json_parse(tmp_path: Path):
    malformed = tmp_path / "badplan.md"
    malformed.write_text("no frontmatter\n", encoding="utf-8")
    report = validate_plan(malformed, strict=False)
    assert report["ok"] is False
    assert report["frontmatter"] == {}
    assert report["errors"][0].startswith("missing frontmatter")
