"""Step definitions for PR compliance checklist in implementation supervisor."""

from pathlib import Path
from typing import Any

from behave import given, then, when

PROJECT_ROOT = Path(__file__).resolve().parents[2]
AGENT_DEF_PATH = PROJECT_ROOT / ".opencode" / "agents" / "implementation-supervisor.md"


@given("the implementation-supervisor.md agent definition exists")
def step_agent_def_exists(context: Any) -> None:
    """Verify the implementation supervisor agent definition file exists."""
    assert AGENT_DEF_PATH.exists(), f"Agent definition not found at {AGENT_DEF_PATH}"
    context.agent_def_path = AGENT_DEF_PATH


@when("I read the implementation supervisor agent definition")
def step_read_agent_def(context: Any) -> None:
    """Read the implementation supervisor agent definition."""
    context.agent_def_content = AGENT_DEF_PATH.read_text(encoding="utf-8")


@then("the worker prompt body includes the PR compliance checklist section")
def step_prompt_includes_checklist(context: Any) -> None:
    """Verify the worker prompt body includes the PR compliance checklist."""
    assert "PR Compliance Checklist" in context.agent_def_content, (
        "Worker prompt body does not include 'PR Compliance Checklist'"
    )


@then("the checklist is marked as MANDATORY")
def step_checklist_is_mandatory(context: Any) -> None:
    """Verify the checklist is marked as MANDATORY."""
    assert "MANDATORY" in context.agent_def_content, (
        "PR Compliance Checklist is not marked as MANDATORY"
    )


@then("the worker prompt body includes a CHANGELOG.md checklist item")
def step_prompt_includes_changelog_item(context: Any) -> None:
    """Verify the worker prompt body includes a CHANGELOG.md checklist item."""
    assert "CHANGELOG.md" in context.agent_def_content, (
        "Worker prompt body does not include a CHANGELOG.md checklist item"
    )


@then("the item instructs workers to add an entry under the Unreleased section")
def step_changelog_item_unreleased(context: Any) -> None:
    """Verify the CHANGELOG.md item mentions the Unreleased section."""
    assert "[Unreleased]" in context.agent_def_content, (
        "CHANGELOG.md checklist item does not mention the [Unreleased] section"
    )


@then("the worker prompt body includes a CONTRIBUTORS.md checklist item")
def step_prompt_includes_contributors_item(context: Any) -> None:
    """Verify the worker prompt body includes a CONTRIBUTORS.md checklist item."""
    assert "CONTRIBUTORS.md" in context.agent_def_content, (
        "Worker prompt body does not include a CONTRIBUTORS.md checklist item"
    )


@then("the item instructs workers to add or update their contribution entry")
def step_contributors_item_add_update(context: Any) -> None:
    """Verify the CONTRIBUTORS.md item instructs workers to add or update."""
    assert "add or update" in context.agent_def_content, (
        "CONTRIBUTORS.md checklist item does not instruct workers to add or update"
    )


@then("the worker prompt body includes a commit footer checklist item")
def step_prompt_includes_commit_footer_item(context: Any) -> None:
    """Verify the worker prompt body includes a commit footer checklist item."""
    assert "Commit footer" in context.agent_def_content, (
        "Worker prompt body does not include a commit footer checklist item"
    )


@then("the item specifies the ISSUES CLOSED footer format")
def step_commit_footer_issues_closed(context: Any) -> None:
    """Verify the commit footer item specifies the ISSUES CLOSED format."""
    assert "ISSUES CLOSED" in context.agent_def_content, (
        "Commit footer checklist item does not specify the ISSUES CLOSED format"
    )


@then("the worker prompt body includes a CI passes checklist item")
def step_prompt_includes_ci_item(context: Any) -> None:
    """Verify the worker prompt body includes a CI passes checklist item."""
    assert "CI passes" in context.agent_def_content, (
        "Worker prompt body does not include a CI passes checklist item"
    )


@then("the item instructs workers to verify all quality gates are green")
def step_ci_item_quality_gates(context: Any) -> None:
    """Verify the CI item instructs workers to verify quality gates."""
    assert "quality gates" in context.agent_def_content, (
        "CI checklist item does not mention quality gates"
    )


@then("the worker prompt body includes a BDD tests checklist item")
def step_prompt_includes_bdd_item(context: Any) -> None:
    """Verify the worker prompt body includes a BDD/Behave tests checklist item."""
    assert "BDD/Behave tests" in context.agent_def_content, (
        "Worker prompt body does not include a BDD/Behave tests checklist item"
    )


@then("the item instructs workers to add or update Behave feature files")
def step_bdd_item_feature_files(context: Any) -> None:
    """Verify the BDD item instructs workers to add or update feature files."""
    assert "added or updated" in context.agent_def_content, (
        "BDD checklist item does not instruct workers to add or update feature files"
    )


@then("the worker prompt body includes an Epic reference checklist item")
def step_prompt_includes_epic_item(context: Any) -> None:
    """Verify the worker prompt body includes an Epic reference checklist item."""
    assert "Epic reference" in context.agent_def_content, (
        "Worker prompt body does not include an Epic reference checklist item"
    )


@then("the item instructs workers to reference the parent Epic issue number")
def step_epic_item_parent_reference(context: Any) -> None:
    """Verify the Epic item instructs workers to reference the parent Epic."""
    assert "parent Epic" in context.agent_def_content, (
        "Epic checklist item does not instruct workers to reference the parent Epic"
    )


@then("the worker prompt body includes a labels checklist item")
def step_prompt_includes_labels_item(context: Any) -> None:
    """Verify the worker prompt body includes a labels checklist item."""
    assert "Labels" in context.agent_def_content, (
        "Worker prompt body does not include a labels checklist item"
    )


@then("the item instructs workers to apply labels via forgejo-label-manager")
def step_labels_item_forgejo_label_manager(context: Any) -> None:
    """Verify the labels item instructs workers to use forgejo-label-manager."""
    assert "forgejo-label-manager" in context.agent_def_content, (
        "Labels checklist item does not mention forgejo-label-manager"
    )


@then("the worker prompt body includes a milestone checklist item")
def step_prompt_includes_milestone_item(context: Any) -> None:
    """Verify the worker prompt body includes a milestone checklist item."""
    assert "Milestone" in context.agent_def_content, (
        "Worker prompt body does not include a milestone checklist item"
    )


@then("the item instructs workers to assign the earliest open milestone")
def step_milestone_item_earliest(context: Any) -> None:
    """Verify the milestone item instructs workers to assign the earliest open milestone."""
    assert "earliest open milestone" in context.agent_def_content, (
        "Milestone checklist item does not mention the earliest open milestone"
    )


@then("the worker prompt body contains all 8 mandatory checklist items")
def step_prompt_contains_all_8_items(context: Any) -> None:
    """Verify the worker prompt body contains all 8 mandatory checklist items."""
    required_items = [
        "CHANGELOG.md",
        "CONTRIBUTORS.md",
        "ISSUES CLOSED",
        "CI passes",
        "BDD/Behave tests",
        "Epic reference",
        "forgejo-label-manager",
        "earliest open milestone",
    ]
    missing = [item for item in required_items if item not in context.agent_def_content]
    assert not missing, (
        f"Worker prompt body is missing the following checklist items: {missing}"
    )
