from __future__ import annotations

import argparse

from ..context import CliContext
from .common import parse_bool_text, raise_config_error


def register(subparsers: argparse._SubParsersAction[argparse.ArgumentParser]) -> None:
    parser = subparsers.add_parser("import", help="导入")
    import_subparsers = parser.add_subparsers(dest="import_command", required=True)

    template = import_subparsers.add_parser("template", help="下载导入模板")
    template.add_argument("--app-key", required=True)
    template.add_argument("--download-to-path")
    template.set_defaults(handler=_handle_template, format_hint="import_template")

    verify = import_subparsers.add_parser("verify", help="校验导入文件")
    verify.add_argument("--app-key", required=True)
    verify.add_argument("--file-path", required=True)
    verify.set_defaults(handler=_handle_verify, format_hint="import_verify")

    repair = import_subparsers.add_parser("repair", help="授权后修复导入文件")
    repair.add_argument("--verification-id", required=True)
    repair.add_argument("--authorized-file-modification", action="store_true")
    repair.add_argument("--output-path")
    repair.add_argument("--repair", dest="selected_repairs", action="append", default=[])
    repair.set_defaults(handler=_handle_repair, format_hint="import_repair")

    start = import_subparsers.add_parser("start", help="启动导入")
    start.add_argument("--app-key", required=True)
    start.add_argument("--verification-id", required=True)
    start.add_argument("--being-enter-auditing", type=parse_bool_text, required=True)
    start.add_argument("--view-key")
    start.set_defaults(handler=_handle_start, format_hint="import_start")

    status = import_subparsers.add_parser("status", help="查询导入状态")
    status.add_argument("--app-key")
    status.add_argument("--import-id")
    status.add_argument("--process-id-str")
    status.set_defaults(handler=_handle_status, format_hint="import_status")


def _handle_template(args: argparse.Namespace, context: CliContext) -> dict:
    return context.imports.record_import_template_get(
        profile=args.profile,
        app_key=args.app_key,
        download_to_path=args.download_to_path,
    )


def _handle_verify(args: argparse.Namespace, context: CliContext) -> dict:
    return context.imports.record_import_verify(
        profile=args.profile,
        app_key=args.app_key,
        file_path=args.file_path,
    )


def _handle_repair(args: argparse.Namespace, context: CliContext) -> dict:
    return context.imports.record_import_repair_local(
        profile=args.profile,
        verification_id=args.verification_id,
        authorized_file_modification=bool(args.authorized_file_modification),
        output_path=args.output_path,
        selected_repairs=list(args.selected_repairs or []),
    )


def _handle_start(args: argparse.Namespace, context: CliContext) -> dict:
    return context.imports.record_import_start(
        profile=args.profile,
        app_key=args.app_key,
        verification_id=args.verification_id,
        being_enter_auditing=bool(args.being_enter_auditing),
        view_key=args.view_key,
    )


def _handle_status(args: argparse.Namespace, context: CliContext) -> dict:
    selectors = [
        bool((args.app_key or "").strip()),
        bool((args.import_id or "").strip()),
        bool((args.process_id_str or "").strip()),
    ]
    if sum(selectors) != 1:
        raise_config_error(
            "import status accepts exactly one selector: --process-id-str, --import-id, or --app-key.",
            fix_hint="Use `--process-id-str` or `--import-id` for a known import, or use only `--app-key` to read the latest import in that app.",
        )
    return context.imports.record_import_status_get(
        profile=args.profile,
        app_key=args.app_key,
        import_id=args.import_id,
        process_id_str=args.process_id_str,
    )
