"""DRS v1.4 compliance test suite."""

from __future__ import annotations

import httpx

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


@register_suite("drs")
class DRSComplianceSuite(ComplianceSuite):
    """Test GA4GH DRS API compliance."""

    spec_name = "drs"
    spec_version = "1.4.0"

    async def test_service_info(self) -> TestResult:
        """GET /ga4gh/drs/v1/service-info returns service info."""
        async with httpx.AsyncClient() as client:
            resp = await client.get(f"{self.base_url}/ga4gh/drs/v1/service-info")
        if resp.status_code != 200:
            return TestResult(
                name="test_service_info",
                status=TestStatus.FAIL,
                message=f"Expected 200, got {resp.status_code}",
                spec=self.spec_name,
                endpoint="/ga4gh/drs/v1/service-info",
            )
        data = resp.json()
        if "type" not in data:
            return TestResult(
                name="test_service_info",
                status=TestStatus.FAIL,
                message="Missing 'type' in service-info",
                spec=self.spec_name,
                endpoint="/ga4gh/drs/v1/service-info",
            )
        return TestResult(
            name="test_service_info",
            status=TestStatus.PASS,
            spec=self.spec_name,
            endpoint="/ga4gh/drs/v1/service-info",
        )

    async def test_get_object_not_found(self) -> TestResult:
        """GET /ga4gh/drs/v1/objects/<id> returns 404 for nonexistent object."""
        async with httpx.AsyncClient() as client:
            resp = await client.get(f"{self.base_url}/ga4gh/drs/v1/objects/nonexistent-id-12345")
        if resp.status_code == 404:
            return TestResult(
                name="test_get_object_not_found",
                status=TestStatus.PASS,
                spec=self.spec_name,
                endpoint="/ga4gh/drs/v1/objects/<id>",
            )
        if resp.status_code == 200:
            return TestResult(
                name="test_get_object_not_found",
                status=TestStatus.PASS,
                message="Server returned 200 (may have matching object)",
                spec=self.spec_name,
                endpoint="/ga4gh/drs/v1/objects/<id>",
            )
        return TestResult(
            name="test_get_object_not_found",
            status=TestStatus.FAIL,
            message=f"Expected 404, got {resp.status_code}",
            spec=self.spec_name,
            endpoint="/ga4gh/drs/v1/objects/<id>",
        )

    async def test_service_info_type_field(self) -> TestResult:
        """Service info type should indicate DRS."""
        async with httpx.AsyncClient() as client:
            resp = await client.get(f"{self.base_url}/ga4gh/drs/v1/service-info")
        if resp.status_code != 200:
            return TestResult(
                name="test_service_info_type_field",
                status=TestStatus.SKIP,
                message="Service info endpoint not available",
                spec=self.spec_name,
                endpoint="/ga4gh/drs/v1/service-info",
            )
        data = resp.json()
        stype = data.get("type", {})
        if isinstance(stype, dict) and stype.get("artifact") == "drs":
            return TestResult(
                name="test_service_info_type_field",
                status=TestStatus.PASS,
                spec=self.spec_name,
                endpoint="/ga4gh/drs/v1/service-info",
            )
        return TestResult(
            name="test_service_info_type_field",
            status=TestStatus.FAIL,
            message=f"Expected type.artifact='drs', got {stype}",
            spec=self.spec_name,
            endpoint="/ga4gh/drs/v1/service-info",
        )

    async def test_json_content_type(self) -> TestResult:
        """Service info should return JSON content type."""
        async with httpx.AsyncClient() as client:
            resp = await client.get(f"{self.base_url}/ga4gh/drs/v1/service-info")
        if resp.status_code != 200:
            return TestResult(
                name="test_json_content_type",
                status=TestStatus.SKIP,
                message="Service info not available",
                spec=self.spec_name,
                endpoint="/ga4gh/drs/v1/service-info",
            )
        ct = resp.headers.get("content-type", "")
        if "application/json" in ct:
            return TestResult(
                name="test_json_content_type",
                status=TestStatus.PASS,
                spec=self.spec_name,
                endpoint="/ga4gh/drs/v1/service-info",
            )
        return TestResult(
            name="test_json_content_type",
            status=TestStatus.FAIL,
            message=f"Expected application/json, got {ct}",
            spec=self.spec_name,
            endpoint="/ga4gh/drs/v1/service-info",
        )
