import json

from graphql import GraphQLSchema

from s2dm import log
from s2dm.exporters.utils.annotated_schema import AnnotatedSchema, TypeMetadata

from .transformer import JsonSchemaTransformer


def transform(
    graphql_schema: GraphQLSchema,
    root_type: str | None = None,
    strict: bool = False,
    type_metadata: dict[str, TypeMetadata] | None = None,
) -> str:
    """
    Transform a GraphQL schema object to JSON Schema format.

    Args:
        graphql_schema: The GraphQL schema object to transform
        strict: Enforce strict field nullability translation from GraphQL to JSON Schema
        root_type: Optional root type name for the JSON schema
        type_metadata: Optional metadata about types produced by instance tag expansion

    Returns:
        str: JSON Schema representation as a string
    """
    log.info(f"Transforming GraphQL schema to JSON Schema with {len(graphql_schema.type_map)} types")

    transformer = JsonSchemaTransformer(graphql_schema, root_type, strict, type_metadata)
    json_schema = transformer.transform()

    json_schema_str = json.dumps(json_schema, indent=2)

    log.info("Successfully converted GraphQL schema to JSON Schema")

    return json_schema_str


def translate_to_jsonschema(
    annotated_schema: AnnotatedSchema,
    root_type: str | None = None,
    strict: bool = False,
) -> str:
    """
    Translate a GraphQL schema file to JSON Schema format.

    Args:
        annotated_schema: The annotated GraphQL schema object to transform
        strict: Enforce strict field nullability translation from GraphQL to JSON Schema
        root_type: Optional root type name for the JSON schema

    Returns:
        str: JSON Schema representation as a string
    """
    return transform(annotated_schema.schema, root_type, strict, annotated_schema.type_metadata)
