from __future__ import annotations

import sys
from pathlib import Path

REPO_ROOT = Path(__file__).resolve().parents[1]

if str(REPO_ROOT) not in sys.path:
    sys.path.insert(0, str(REPO_ROOT))

from src.busopt.config import ModelConfig, add_model_config_arguments, model_config_from_namespace
from src.busopt.runner import (
    run_nested_gbd,
    run_nested_gbd_hpr,
    run_nested_gbd_hpr_semi,
    run_nested_gbd_with_fixed_upper_solution,
    save_result,
)


def run_nested_gbd_and_save(config: ModelConfig, output_path: Path) -> None:
    result = run_nested_gbd(config)
    save_result(result, output_path)


def run_nested_gbd_hpr_and_save(config: ModelConfig, output_path: Path) -> None:
    result = run_nested_gbd_hpr(config)
    save_result(result, output_path)


def run_nested_gbd_hpr_semi_and_save(config: ModelConfig, output_path: Path) -> None:
    result = run_nested_gbd_hpr_semi(config)
    save_result(result, output_path)


def run_nested_gbd_with_fixed_upper_solution_and_save(
    config: ModelConfig,
    fixed_upper_solution: dict[str, object],
    output_path: Path,
    *,
    enforce_min_freq_when_selected: bool = True,
    require_positive_profit: bool = True,
) -> None:
    result = run_nested_gbd_with_fixed_upper_solution(
        config,
        fixed_upper_solution,
        enforce_min_freq_when_selected=enforce_min_freq_when_selected,
        require_positive_profit=require_positive_profit,
    )
    save_result(result, output_path)


def build_base_grid() -> list[tuple[int, int, list[float]]]:
    return [
        (30, 4, [0, 1, 2, 4, 6]),
        (30, 5, [0, 1, 2, 4, 6]),
        (30, 6, [0, 1, 2, 4]),
        (30, 7, [0, 1, 2, 4]),
        (30, 8, [0, 1, 2]),
        (30, 9, [0, 1, 2]),
        (50, 4, [0, 1, 2, 4, 6]),
        (50, 5, [0, 1, 2, 4, 6]),
        (50, 6, [0, 1, 2, 4]),
        (50, 7, [0, 1, 2, 4]),
        (50, 8, [0, 1, 2]),
        (50, 9, [0, 1, 2]),
    ]
