from pathlib import Path
from typing import List


def pack_n_latest_logs_for_scenario_in_dir(scenario_name, scenario_log_dir: Path, n) -> List[Path]:
    """ List the `n` newest scenario log files in the given `scenario_log_dir`."""
    if n == 0:
        return []
    # Get all scenario run logs, sort and reverse them (newest first)
    scenario_logs = [
        path for path in scenario_log_dir.iterdir() if (path.is_file() and "-run_" in path.name)
    ]
    history = sorted(scenario_logs, reverse=True)

    # Can't pack more than the number of available logs.
    num_of_packable_iterations = min(n, len(scenario_logs))
    print(scenario_logs)
    print(n, len(scenario_logs), num_of_packable_iterations)

    if not history:
        raise RuntimeError(f"No Scenario logs found in {scenario_log_dir}")

    if num_of_packable_iterations < n:
        # We ran out of scenario logs to add before reaching the requested number of n latest logs.
        print(
            f"Only packing {num_of_packable_iterations} logs of requested latest {n} "
            f"- no more logs found for {scenario_name}!"
        )

    return history[:num_of_packable_iterations]