import argparse
from pathlib import Path

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


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:")
    if Path(config.procedure_directory).is_dir():
        test_configs = [f.name for f in Path(config.procedure_directory).iterdir()]
        for c in test_configs:
            if "yaml" in c:
                print("  - " + str.replace(c, ".yaml", ""))
    else:
        print(f"{config.procedure_directory} is missing")

    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",
    )

    check_parser = commands.add_parser(
        "check",
        description="Runs a single check",
        help="Runs a single check",
    )
    check_parser.add_argument("check_name")

    benchmark_parser = commands.add_parser(
        "benchmark",
        description="Runs a single benchmark",
        help="Runs a single benchmark",
    )
    benchmark_parser.add_argument("benchmark_name")

    run_parser = commands.add_parser(
        "run",
        description="Runs a single full test procedure",
        help="Runs a single full test procedure",
    )
    run_parser.add_argument(
        "procedure_name",
        help="the name of a procedure stored in /etc/test-automation-suite\
        without its .yaml extension.",
    )
    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_false",
        help="enable debug logging",
    )
    parser.add_argument(
        "-c",
        "--config",
        type=Path,
        help="path to the config.yaml file",
    )

    return parser
