"""MCP resource definitions for GA4GH specifications.

Resources expose GA4GH specification metadata and reference data
for LLM consumption.
"""

from __future__ import annotations

import json

from mcp.types import Resource

SPEC_DESCRIPTIONS: dict[str, dict[str, str]] = {
    "beacon": {
        "name": "Beacon v2",
        "description": "GA4GH Beacon v2 API for variant and biomedical data queries.",
        "version": "2.0.0",
        "url": "https://beacon-project.io/",
    },
    "drs": {
        "name": "Data Repository Service (DRS)",
        "description": "GA4GH DRS for accessing genomic data objects.",
        "version": "1.4.0",
        "url": "https://ga4gh.github.io/data-repository-service-schemas/",
    },
    "htsget": {
        "name": "htsget",
        "description": "GA4GH htsget for streaming reads and variants.",
        "version": "1.3.0",
        "url": "https://samtools.github.io/hts-specs/htsget.html",
    },
    "refget": {
        "name": "refget",
        "description": "GA4GH refget for reference sequence retrieval.",
        "version": "2.0.0",
        "url": "https://samtools.github.io/hts-specs/refget.html",
    },
    "phenopackets": {
        "name": "Phenopackets",
        "description": "GA4GH Phenopackets for patient phenotype exchange.",
        "version": "2.0.0",
        "url": "https://phenopacket-schema.readthedocs.io/",
    },
    "vrs": {
        "name": "Variant Representation (VRS)",
        "description": "GA4GH VRS for precise variant representation and identification.",
        "version": "2.0.0",
        "url": "https://vrs.ga4gh.org/",
    },
    "wes": {
        "name": "Workflow Execution Service (WES)",
        "description": "GA4GH WES for submitting and managing workflow runs.",
        "version": "1.1.0",
        "url": "https://ga4gh.github.io/workflow-execution-service-schemas/",
    },
    "tes": {
        "name": "Task Execution Service (TES)",
        "description": "GA4GH TES for running batch compute tasks.",
        "version": "1.1.0",
        "url": "https://ga4gh.github.io/task-execution-schemas/",
    },
    "trs": {
        "name": "Tool Registry Service (TRS)",
        "description": "GA4GH TRS for discovering tools and workflows.",
        "version": "2.0.1",
        "url": "https://ga4gh.github.io/tool-registry-service-schemas/",
    },
    "data_connect": {
        "name": "Data Connect",
        "description": "GA4GH Data Connect for SQL-like querying of tabular genomic data.",
        "version": "1.0.0",
        "url": "https://github.com/ga4gh-discovery/data-connect/",
    },
    "passports": {
        "name": "GA4GH Passports",
        "description": "GA4GH Passports for federated identity and data access authorization.",
        "version": "1.2.0",
        "url": "https://github.com/ga4gh-duri/ga4gh-duri.github.io/",
    },
    "duo": {
        "name": "Data Use Ontology (DUO)",
        "description": "GA4GH DUO for machine-readable data use conditions.",
        "version": "2.0.0",
        "url": "https://github.com/EBISPOT/DUO/",
    },
    "crypt4gh": {
        "name": "Crypt4GH",
        "description": "GA4GH Crypt4GH for encrypting genomic data files.",
        "version": "1.0.0",
        "url": "https://samtools.github.io/hts-specs/crypt4gh.html",
    },
    "pedigree": {
        "name": "Pedigree",
        "description": "GA4GH Pedigree for family relationship representation.",
        "version": "1.0.0",
        "url": "https://github.com/GA4GH-Pedigree-Standard/",
    },
}


def list_resources() -> list[Resource]:
    """Return all GA4GH resources."""
    resources = [
        Resource(
            uri="ga4gh://specs",
            name="GA4GH Specifications Index",
            description="List all supported GA4GH specifications.",
            mimeType="application/json",
        ),
    ]
    for spec_id, spec_info in SPEC_DESCRIPTIONS.items():
        resources.append(
            Resource(
                uri=f"ga4gh://specs/{spec_id}",
                name=spec_info["name"],
                description=spec_info["description"],
                mimeType="application/json",
            )
        )
    return resources


def get_resource(uri: str) -> str:
    """Get a GA4GH resource by URI."""
    if uri == "ga4gh://specs":
        return json.dumps(
            {
                "specifications": list(SPEC_DESCRIPTIONS.values()),
                "total": len(SPEC_DESCRIPTIONS),
            },
            indent=2,
        )

    if uri.startswith("ga4gh://specs/"):
        spec_id = uri.removeprefix("ga4gh://specs/")
        spec_info = SPEC_DESCRIPTIONS.get(spec_id)
        if spec_info:
            return json.dumps(spec_info, indent=2)
        return json.dumps({"error": f"Unknown specification: {spec_id}"})

    return json.dumps({"error": f"Unknown resource URI: {uri}"})
