# Auto-generated by Fixit. Do not edit by hand.
import httpx
import pytest
from fixit.core.config import read_config

cfg = read_config()
BASE_URL = (cfg.base_url if cfg else "http://localhost:8000").rstrip("/")

TEST_CASES = [
    {
        "id": "post_/api/users_happy_path",
        "name": "POST /api/users - Happy Path",
        "method": "POST",
        "path": "/api/users",
        "expected_status": 201,
        "headers": {'Content-Type': 'application/json'},
        "path_params": {},
        "query_params": {},
        "request_body": {'email': 'test_string', 'password': 'test_string', 'name': 'test_string'},
    },
    {
        "id": "post_/api/auth/login_happy_path",
        "name": "POST /api/auth/login - Happy Path",
        "method": "POST",
        "path": "/api/auth/login",
        "expected_status": 200,
        "headers": {'Content-Type': 'application/json'},
        "path_params": {},
        "query_params": {},
        "request_body": {'email': 'test_string', 'password': 'test_string'},
    },
    {
        "id": "get_/api/users/{id}_happy_path",
        "name": "GET /api/users/{id} - Happy Path",
        "method": "GET",
        "path": "/api/users/{id}",
        "expected_status": 200,
        "headers": {'Content-Type': 'application/json', 'Authorization': 'Bearer test_token'},
        "path_params": {'id': 123},
        "query_params": {},
        "request_body": None,
    },
    {
        "id": "get_/api/users/{id}_invalid_id_type",
        "name": "GET /api/users/{id} - Invalid id Type",
        "method": "GET",
        "path": "/api/users/{id}",
        "expected_status": 400,
        "headers": {'Content-Type': 'application/json', 'Authorization': 'Bearer test_token'},
        "path_params": {'id': 'not_an_integer'},
        "query_params": {},
        "request_body": None,
    },
    {
        "id": "get_/api/users/{id}_unauthorized",
        "name": "GET /api/users/{id} - Unauthorized",
        "method": "GET",
        "path": "/api/users/{id}",
        "expected_status": 401,
        "headers": {'Content-Type': 'application/json'},
        "path_params": {'id': 123},
        "query_params": {},
        "request_body": None,
    },
    {
        "id": "get_/health_happy_path",
        "name": "GET /health - Happy Path",
        "method": "GET",
        "path": "/health",
        "expected_status": 200,
        "headers": {'Content-Type': 'application/json'},
        "path_params": {},
        "query_params": {},
        "request_body": None,
    },
]

@pytest.mark.parametrize("case", TEST_CASES, ids=[c["id"] for c in TEST_CASES])
def test_endpoint(case):
    path = case["path"]
    # format path parameters: OpenAPI style /users/{id}
    if case.get("path_params"):
        try:
            path = path.format(**case["path_params"])
        except Exception:
            pass
    url = BASE_URL + path
    headers = dict(case.get("headers") or {})
    params = case.get("query_params") or None
    data = case.get("request_body", None)
    method = case["method"].lower()

    with httpx.Client(follow_redirects=True, timeout=10.0) as client:
        resp = client.request(method, url, json=data, params=params, headers=headers)

    try:
        assert resp.status_code == case["expected_status"], (
            f"{case['id']}: {method.upper()} {url} "
            f"got {resp.status_code}, body={resp.text[:500]!r}"
        )
    except AssertionError:
        import json, pathlib
        ws = pathlib.Path(".fixit"); ws.mkdir(exist_ok=True)
        (ws / "reports").mkdir(exist_ok=True)
        failure = {
            "test_id": case["id"],
            "endpoint_id": f"{case['method']} {case['path']}",
            "method": case["method"],
            "path": case["path"],
            "expected_status": case["expected_status"],
            "actual_status": resp.status_code,
            "request": {
                "headers": {k: ("<redacted>" if k.lower()=="authorization" else v) for k,v in (headers or {}).items()},
                "path_params": case.get("path_params"),
                "query_params": case.get("query_params"),
                "body": case.get("request_body"),
            },
            "response": {
                "headers": dict(resp.headers),
                "body_text": resp.text[:400],
            },
            "framework": "unknown"
        }
        with open(ws / "reports" / "failures.jsonl", "a", encoding="utf-8") as f:
            f.write(json.dumps(failure, ensure_ascii=False) + "\n")
        raise