from __future__ import annotations

import json


def register(subparsers) -> None:
    schema_parser = subparsers.add_parser("schema", help="查看或演进字段定义")
    schema_subparsers = schema_parser.add_subparsers(dest="schema_action", required=True)

    inspect_parser = schema_subparsers.add_parser("inspect", help="查看当前 schema")
    inspect_parser.set_defaults(handler=handle_inspect)

    evolve_parser = schema_subparsers.add_parser("evolve", help="新增动态字段")
    evolve_parser.add_argument("--labels", nargs="*", default=[])
    evolve_parser.add_argument("--json", dest="payload_json")
    evolve_parser.set_defaults(handler=handle_evolve)


def handle_inspect(args, service):
    return service.inspect_schema()


def handle_evolve(args, service):
    labels = list(args.labels)
    if args.payload_json:
        payload = json.loads(args.payload_json)
        labels.extend(payload.get("labels", []))
    return service.evolve_schema(labels)
