import argparse
from pathlib import Path

import argcomplete

from test_automation_suite.benchmarks import benchmarks_by_name
from test_automation_suite.checks import checks_by_name
from test_automation_suite.core.config import Config
from test_automation_suite.procedures import procedures_by_name


def dir_path_type(value: str) -> Path:
    path = Path(value)
    if path.is_dir():
        return path
    raise NotADirectoryError(value)


def print_pcb(config: Config) -> None:
    """Prints all available procedures, checks, and benchmarks."""
    print("Procedures:")
    for procedure in procedures_by_name:
        print("  - " + procedure)

    print()
    print("Checks:")
    for check in checks_by_name:
        print("  - " + check)

    print()
    print("Benchmarks:")
    for benchmark in benchmarks_by_name:
        print("  - " + benchmark)


def argument_parser() -> argparse.ArgumentParser:
    parser = argparse.ArgumentParser(
        prog="Test Automation Suite",
        description="Framework for automatic testing purposes",
    )
    commands = parser.add_subparsers(dest="command", title="commands")

    commands.add_parser(
        "list",
        description="Lists available checks, benchmarks and procedures",
        help="Lists available checks, benchmarks and procedures",
    )

    run_parser = commands.add_parser(
        "run",
        description="Runs a task",
        help="Runs a task",
    )
    run_parser.add_argument(
        "task_name",
        help="the name of a task (use `list` command to show which are available)",
        choices=sorted(
            checks_by_name.keys() | benchmarks_by_name.keys() | procedures_by_name.keys()
        ),
    )
    run_parser.add_argument(
        "-p",
        "--prepare-only",
        action="store_true",
        help="Only prepare the test run and start after the next reboot",
    )

    parser.add_argument(
        "-v",
        "--verbose",
        action="store_true",
        help="enable debug logging",
    )
    parser.add_argument(
        "-c",
        "--config",
        type=Path,
        help="path to the config.yaml file",
    )
    argcomplete.autocomplete(parser)
    return parser
