from __future__ import annotations

import json


def register(subparsers) -> None:
    customer_parser = subparsers.add_parser("customer", help="客户增删改查")
    customer_subparsers = customer_parser.add_subparsers(dest="customer_action", required=True)

    add_parser = customer_subparsers.add_parser("add", help="新增客户")
    add_parser.add_argument("--json", dest="payload_json", required=True)
    add_parser.set_defaults(handler=handle_add)

    update_parser = customer_subparsers.add_parser("update", help="更新客户")
    update_parser.add_argument("--match-json", required=True)
    update_parser.add_argument("--updates-json", required=True)
    update_parser.set_defaults(handler=handle_update)

    delete_parser = customer_subparsers.add_parser("delete", help="删除客户")
    delete_parser.add_argument("--match-json", required=True)
    delete_parser.set_defaults(handler=handle_delete)

    query_parser = customer_subparsers.add_parser("query", help="查询客户")
    query_parser.add_argument("--json", dest="payload_json", required=True)
    query_parser.set_defaults(handler=handle_query)

    record_add_parser = customer_subparsers.add_parser("record-add", help="新增业务记录")
    record_add_parser.add_argument("--json", dest="payload_json", required=True)
    record_add_parser.set_defaults(handler=handle_record_add)

    record_query_parser = customer_subparsers.add_parser("record-query", help="查询业务记录")
    record_query_parser.add_argument("--json", dest="payload_json", required=True)
    record_query_parser.set_defaults(handler=handle_record_query)


def handle_add(args, service):
    return service.add_customer(json.loads(args.payload_json))


def handle_update(args, service):
    return service.update_customer(
        json.loads(args.match_json),
        json.loads(args.updates_json),
    )


def handle_delete(args, service):
    return service.delete_customer(json.loads(args.match_json))


def handle_query(args, service):
    return service.query_customers(json.loads(args.payload_json))


def handle_record_add(args, service):
    return service.add_record(json.loads(args.payload_json))


def handle_record_query(args, service):
    return service.query_records(json.loads(args.payload_json))
