# Path: dhis2_core/src/dhis2/generate/json_schema.py
# def generate_json_schema_metadata(dhis2_schemas: List[Schema]):
#     schema = {
#         "$id": "https://dhis2.org/schemas/metadata",
#         "$schema": "http://json-schema.org/draft-07/schema#",
#         "type": "object",
#         "definitions": {
#             "identifiableObject": {
#                 "oneOf": [
#                     {"type": "object", "properties": {"id": {"type": "string"}}},
#                     {"type": "object", "properties": {"code": {"type": "string"}}},
#                 ]
#             }
#         },
#         "properties": {},
#         "required": [],
#     }
# 
#     for dhis2_schema in dhis2_schemas:
#         if not dhis2_schema.metadata:
#             continue
# 
#         property = {"type": "array", "items": {"$ref": f"#/definitions/{dhis2_schema.name}"}}
#         schema.get("properties")[dhis2_schema.plural] = property
# 
#     for dhis2_schema in dhis2_schemas:
#         if not dhis2_schema.metadata and not dhis2_schema.embeddedObject:
#             continue
# 
#         property = handle_object(dhis2_schema)
#         schema.get("definitions")[dhis2_schema.singular] = property
# 
#     return schema


import json
import logging
import click
from dhis2.core.inventory import resolve_one
from dhis2.core.utils import load_and_parse_schema
from .json_schema import generate_json_schema_metadata



log = logging.getLogger(__name__)


@click.group("generate")
def cli_generator():
    """ Various commands for generating data/schemas """
    pass


@cli_generator.command("json_schemas")
@click.argument("host-id")
@click.pass_obj
def cmd_json_schemas(ctx, host_id: str):
    """ Generate JSON Schemas from a dhis2 instance """
    host = resolve_one(host_id, ctx.inventory)
    schemas = load_and_parse_schema(host)

    if schemas:

# Path: dhis2_core/src/dhis2/generate/json_schema.py
# def generate_json_schema_metadata(dhis2_schemas: List[Schema]):
#     schema = {
#         "$id": "https://dhis2.org/schemas/metadata",
#         "$schema": "http://json-schema.org/draft-07/schema#",
#         "type": "object",
#         "definitions": {
#             "identifiableObject": {
#                 "oneOf": [
#                     {"type": "object", "properties": {"id": {"type": "string"}}},
#                     {"type": "object", "properties": {"code": {"type": "string"}}},
#                 ]
#             }
#         },
#         "properties": {},
#         "required": [],
#     }
# 
#     for dhis2_schema in dhis2_schemas:
#         if not dhis2_schema.metadata:
#             continue
# 
#         property = {"type": "array", "items": {"$ref": f"#/definitions/{dhis2_schema.name}"}}
#         schema.get("properties")[dhis2_schema.plural] = property
# 
#     for dhis2_schema in dhis2_schemas:
#         if not dhis2_schema.metadata and not dhis2_schema.embeddedObject:
#             continue
# 
#         property = handle_object(dhis2_schema)
#         schema.get("definitions")[dhis2_schema.singular] = property
# 
#     return schema


# Path: dhis2_core/src/dhis2/generate/cli.py
import json
import logging
import click
from dhis2.core.inventory import resolve_one
from dhis2.core.utils import load_and_parse_schema
from .json_schema import generate_json_schema_metadata




log = logging.getLogger(__name__)


@click.group("generate")
def cli_generator():
    """ Various commands for generating data/schemas """
    pass


@cli_generator.command("json_schemas")
@click.argument("host-id")
@click.pass_obj
def cmd_json_schemas(ctx, host_id: str):
    """ Generate JSON Schemas from a dhis2 instance """
    host = resolve_one(host_id, ctx.inventory)
    schemas = load_and_parse_schema(host)

    if schemas:

        click.echo(json.dumps(generate_json_schema_metadata(schemas), indent=2))