"""htsget v1.3 compliance test suite."""

from __future__ import annotations

import httpx

from ai4gh.compliance.runner import ComplianceSuite, TestResult, TestStatus, register_suite


@register_suite("htsget")
class HtsgetComplianceSuite(ComplianceSuite):
    """Test GA4GH htsget API compliance."""

    spec_name = "htsget"
    spec_version = "1.3.0"

    async def test_reads_endpoint_exists(self) -> TestResult:
        """GET /reads/<id> endpoint exists."""
        async with httpx.AsyncClient() as client:
            resp = await client.get(f"{self.base_url}/reads/nonexistent")
        # 404 or 400 means the endpoint exists but ID wasn't found
        if resp.status_code in (200, 400, 404, 401, 403):
            return TestResult(
                name="test_reads_endpoint_exists",
                status=TestStatus.PASS,
                spec=self.spec_name,
                endpoint="/reads/<id>",
            )
        return TestResult(
            name="test_reads_endpoint_exists",
            status=TestStatus.FAIL,
            message=f"Unexpected status {resp.status_code}",
            spec=self.spec_name,
            endpoint="/reads/<id>",
        )

    async def test_variants_endpoint_exists(self) -> TestResult:
        """GET /variants/<id> endpoint exists."""
        async with httpx.AsyncClient() as client:
            resp = await client.get(f"{self.base_url}/variants/nonexistent")
        if resp.status_code in (200, 400, 404, 401, 403):
            return TestResult(
                name="test_variants_endpoint_exists",
                status=TestStatus.PASS,
                spec=self.spec_name,
                endpoint="/variants/<id>",
            )
        return TestResult(
            name="test_variants_endpoint_exists",
            status=TestStatus.FAIL,
            message=f"Unexpected status {resp.status_code}",
            spec=self.spec_name,
            endpoint="/variants/<id>",
        )

    async def test_json_content_type(self) -> TestResult:
        """Responses should use application/json content type."""
        async with httpx.AsyncClient() as client:
            resp = await client.get(f"{self.base_url}/reads/nonexistent")
        ct = resp.headers.get("content-type", "")
        if "json" in ct or resp.status_code in (401, 403):
            return TestResult(
                name="test_json_content_type",
                status=TestStatus.PASS,
                spec=self.spec_name,
                endpoint="/reads/<id>",
            )
        return TestResult(
            name="test_json_content_type",
            status=TestStatus.FAIL,
            message=f"Expected JSON content type, got {ct}",
            spec=self.spec_name,
            endpoint="/reads/<id>",
        )
