#!/usr/bin/env python3
from __future__ import annotations

import importlib.util
from pathlib import Path


HERE = Path(__file__).resolve().parent


def load(name: str, path: Path):
    spec = importlib.util.spec_from_file_location(name, path)
    module = importlib.util.module_from_spec(spec)
    assert spec.loader is not None
    spec.loader.exec_module(module)
    return module


def main() -> None:
    controller = load("p6controller", HERE / "opprof_phase6_controller.py")
    solo = load("p6solo", HERE / "opprof_phase6_solo_controller.py")
    analysis = load("p6analysis", HERE / "analyze_phase6.py")
    client = load("p6client", HERE / "opprof_phase6_client.py")
    assert len(controller.CELLS) == 12
    primary = sum(3 if item.get("trap") else 2 for item in controller.CELLS.values())
    assert primary == 25
    assert sum(len(gpus) for _name, wave, _cost in controller.WAVES for _cell, gpus in wave) == 28
    scores = {"a": 1.0, "b": 1.0, "c": 2.0}
    tol, buckets = analysis.floor_buckets(scores)
    assert tol == 2e-6 and buckets["a"] == buckets["b"] < buckets["c"]
    tau = analysis.kendall_tau_b({"a": 0, "b": 1, "c": 2}, {"a": 0, "b": 1, "c": 2})
    assert tau["tau_b"] == 1.0 and tau["concordant"] == 3
    assert len(solo.ORDER) == 12 and set(solo.ORDER) == set(controller.CELLS)
    assert sum(len(items) for items in solo.CORE.values()) == 23
    assert sum(2 if len(items) == 1 else len(items) for items in solo.CORE.values()) == 25
    assert solo.GPU_LIMIT == 6.0
    assert sum(solo.CELL_ESTIMATE.values()) + solo.SAFETY_HOURS == 3.44
    assert solo.next_below([.1, .2, .3], {.2, .3}) == .1
    assert solo.next_above([.1, .2, .3], {.1, .2}) == .3
    parsed = client.parser().parse_args([
        "run-anchor",
        "--study", "study.json",
        "--cell", "tp4_mns16",
        "--anchor", "0.5",
        "--tp", "4",
        "--mns", "16",
        "--base-url", "http://127.0.0.1:8000",
        "--result-dir", "out",
        "--disable-slo-early-stop",
    ])
    assert parsed.disable_slo_early_stop is True
    print("phase6 tools: PASS")


if __name__ == "__main__":
    main()
