"""
Erasus CLI — Entry point for command-line usage.

Usage::

    erasus unlearn --config config.yaml
    erasus evaluate --checkpoint model.pt --metrics accuracy mia
    erasus --help

Or as a module::

    python -m erasus.cli.main unlearn --config config.yaml
"""

from __future__ import annotations

import argparse
import sys


def main() -> None:
    parser = argparse.ArgumentParser(
        prog="erasus",
        description=(
            "ERASUS — Efficient Representative And Surgical Unlearning Selection\n"
            "Universal Machine Unlearning via Coreset Selection"
        ),
        formatter_class=argparse.RawDescriptionHelpFormatter,
    )

    subparsers = parser.add_subparsers(
        title="commands",
        dest="command",
        help="Available commands",
    )

    # Register sub-commands
    from erasus.cli.unlearn import add_parser as add_unlearn
    from erasus.cli.evaluate import add_parser as add_evaluate

    add_unlearn(subparsers)
    add_evaluate(subparsers)

    # Benchmark sub-command
    from erasus.cli.benchmark import add_benchmark_args, run_benchmark
    benchmark_parser = subparsers.add_parser("benchmark", help="Run benchmarks.")
    add_benchmark_args(benchmark_parser)
    benchmark_parser.set_defaults(func=run_benchmark)

    # Visualize sub-command
    from erasus.cli.visualize import add_visualize_args, run_visualize
    visualize_parser = subparsers.add_parser("visualize", help="Generate visualizations.")
    add_visualize_args(visualize_parser)
    visualize_parser.set_defaults(func=run_visualize)

    from erasus.cli.service import add_service_args, run_service
    service_parser = subparsers.add_parser("service", help="Run the FastAPI service.")
    add_service_args(service_parser)
    service_parser.set_defaults(func=run_service)

    from erasus.cli.profile import add_profile_args, run_profile
    profile_parser = subparsers.add_parser(
        "profile",
        help="Time/memory breakdown for selector + unlearning on synthetic data.",
    )
    add_profile_args(profile_parser)
    profile_parser.set_defaults(func=run_profile)

    # Legacy --config support (redirect to unlearn)
    parser.add_argument(
        "--config", "-c", type=str, default=None,
        help="(Legacy) YAML config — equivalent to 'erasus unlearn --config'.",
    )
    parser.add_argument(
        "--dry-run", action="store_true",
        help="(Legacy) Validate only — equivalent to 'erasus unlearn --dry-run'.",
    )

    args = parser.parse_args()

    # Handle legacy --config flag
    if args.command is None and args.config:
        # Redirect to unlearn sub-command
        sys.argv = ["erasus", "unlearn", "--config", args.config]
        if args.dry_run:
            sys.argv.append("--dry-run")
        args = parser.parse_args(sys.argv[1:])

    if args.command is None:
        parser.print_help()
        sys.exit(0)

    # Dispatch to the selected sub-command
    args.func(args)


if __name__ == "__main__":
    main()
