"""Tests for maintenance tree type."""
import pytest
from httpx import AsyncClient


@pytest.mark.asyncio
async def test_create_maintenance_tree(client: AsyncClient, auth_headers: dict):
    """Maintenance tree type is accepted by the API."""
    resp = await client.post(
        "/api/v1/trees",
        json={
            "name": "Update FSLogix",
            "description": "Monthly FSLogix update procedure",
            "tree_type": "maintenance",
            "tree_structure": {
                "steps": [
                    {"id": "step-1", "type": "procedure_step", "title": "Download installer",
                     "description": "Get latest FSLogix from Microsoft", "content_type": "action"},
                    {"id": "step-end", "type": "procedure_end", "title": "Complete"},
                ]
            },
        },
        headers=auth_headers,
    )
    assert resp.status_code == 201, resp.text
    data = resp.json()
    assert data["tree_type"] == "maintenance"


@pytest.mark.asyncio
async def test_list_maintenance_trees_filter(client: AsyncClient, auth_headers: dict):
    """Filtering by tree_type=maintenance returns only maintenance trees."""
    await client.post(
        "/api/v1/trees",
        json={
            "name": "Maintenance Only",
            "tree_type": "maintenance",
            "tree_structure": {
                "steps": [
                    {"id": "s1", "type": "procedure_step", "title": "Step",
                     "description": "Do it", "content_type": "action"},
                    {"id": "end", "type": "procedure_end", "title": "Done"},
                ]
            },
        },
        headers=auth_headers,
    )
    resp = await client.get("/api/v1/trees?tree_type=maintenance", headers=auth_headers)
    assert resp.status_code == 200
    trees = resp.json()
    assert all(t["tree_type"] == "maintenance" for t in trees)
    assert len(trees) >= 1


@pytest.mark.asyncio
async def test_invalid_tree_type_rejected(client: AsyncClient, auth_headers: dict):
    """An unrecognized tree_type value is rejected with 422."""
    resp = await client.post(
        "/api/v1/trees",
        json={
            "name": "Bad Type",
            "tree_type": "garbage",
            "tree_structure": {
                "steps": [
                    {"id": "s1", "type": "procedure_step", "title": "Step",
                     "description": "Do it", "content_type": "action"},
                    {"id": "end", "type": "procedure_end", "title": "Done"},
                ]
            },
        },
        headers=auth_headers,
    )
    assert resp.status_code == 422
