"""Tests for the GET /api/claims/{id}/serialize-837 endpoint (SP8)."""
import io
from pathlib import Path

from fastapi.testclient import TestClient

from cyclone.api import app


def _seed_claim(client: TestClient) -> str:
    fixture = Path("tests/fixtures/co_medicaid_837p.txt").read_text()
    r = client.post(
        "/api/parse-837",
        files={"file": ("claim.txt", io.BytesIO(fixture.encode()), "text/plain")},
        headers={"Accept": "application/json"},
    )
    assert r.status_code == 200, r.text
    claim_id = r.json()["claims"][0]["claim_id"]
    return claim_id


def test_endpoint_returns_x12_attachment():
    with TestClient(app) as client:
        claim_id = _seed_claim(client)
        r = client.get(f"/api/claims/{claim_id}/serialize-837")
    assert r.status_code == 200
    assert r.headers["content-type"].startswith("text/x12")
    cd = r.headers["content-disposition"]
    assert "attachment" in cd
    assert f"claim-{claim_id}.x12" in cd
    assert r.text.startswith("ISA*")


def test_endpoint_404_for_missing_claim():
    with TestClient(app) as client:
        r = client.get("/api/claims/CLM-DOES-NOT-EXIST/serialize-837")
    assert r.status_code == 404
    body = r.json()
    assert body["error"] == "Not found"
    assert "CLM-DOES-NOT-EXIST" in body["detail"]


def test_endpoint_regenerated_text_round_trips_through_parser():
    """The endpoint's body should be a parseable 837 — round-trip test."""
    from cyclone.parsers.parse_837 import parse
    from cyclone.parsers.payer import PayerConfig

    with TestClient(app) as client:
        claim_id = _seed_claim(client)
        r = client.get(f"/api/claims/{claim_id}/serialize-837")
    assert r.status_code == 200
    result = parse(r.text, PayerConfig(name="CO_MEDICAID"))
    assert result.claims, "endpoint body didn't parse back to any claims"
    assert result.claims[0].claim.claim_id == claim_id