#!/usr/bin/env python3
"""Versioned Stage-B v3 plan and locked-confirmation contracts."""

from pathlib import Path
import sys
import unittest


CODE_ROOT = Path(__file__).resolve().parents[1]
sys.path.insert(0, str(CODE_ROOT))

from experiments.plan import build_trial_plan, load_document  # noqa: E402


CONFIG_ROOT = CODE_ROOT / "config" / "experiments"


class BilateralNetworkV3ConfigTest(unittest.TestCase):
    def test_screening_grid_is_directional_paired_and_bounded(self):
        plan = build_trial_plan(
            load_document(
                CONFIG_ROOT / "bilateral_network_v3_screening.json"
            )
        )

        self.assertEqual(plan["pair_count"], 30)
        self.assertEqual(plan["trial_count"], 30)
        self.assertEqual(
            {trial["method"]["method_id"] for trial in plan["trials"]},
            {"proposed_energy"},
        )
        self.assertEqual(
            {
                trial["trajectory"]["trajectory_id"]
                for trial in plan["trials"]
            },
            {
                "free_space_network_roundtrip",
                "slow_contact_network_roundtrip",
            },
        )
        self.assertEqual(
            {
                trial["factors"]["network_profile"]["profile_id"]
                for trial in plan["trials"]
            },
            {
                "nominal",
                "symmetric_delay_40ms",
                "asymmetric_delay_20_60ms",
                "symmetric_40ms_jitter_4ms",
                "symmetric_40ms_loss_2pct",
            },
        )
        self.assertTrue(
            all(
                trial["factors"]["bilateral_pair_network_profiles"]
                and trial["factors"]["network_common_random_numbers"]
                for trial in plan["trials"]
            )
        )

    def test_locked_confirmation_uses_disjoint_data_root(self):
        calibration = load_document(
            CONFIG_ROOT
            / "bilateral_calibration_v3_stable_contact.json"
        )
        calibration_data_root = calibration["factors"][
            "bilateral_data_root_seed"
        ][0]
        locked_names = (
            "bilateral_locked_v3_stable_contact.json",
            "bilateral_locked_v3_energy_challenge.json",
        )
        locked_data_roots = set()
        for name in locked_names:
            specification = load_document(CONFIG_ROOT / name)
            plan = build_trial_plan(specification)
            self.assertEqual(plan["split"], "locked")
            self.assertEqual(plan["pair_count"], 20)
            self.assertEqual(plan["trial_count"], 20)
            locked_data_roots.add(
                specification["factors"]["bilateral_data_root_seed"][0]
            )

        self.assertEqual(len(locked_data_roots), 1)
        self.assertNotIn(calibration_data_root, locked_data_roots)

    def test_network_metric_order_freezes_paired_gate_names(self):
        configuration = load_document(
            CONFIG_ROOT
            / "metrics_bilateral_network_v3_screening.json"
        )
        self.assertEqual(
            configuration["enabled"],
            ["h3", "h4", "bilateral", "network"],
        )
        paired = configuration["network"]["paired_gates"]
        self.assertEqual(paired["nominal_profile_id"], "nominal")
        self.assertEqual(
            paired[
                "maximum_tracking_rmse_delta_vs_nominal_rad"
            ],
            0.005,
        )
        self.assertEqual(
            paired[
                "maximum_abs_contact_rms_relative_change_vs_nominal"
            ],
            0.1,
        )

    def test_locked_network_grid_uses_disjoint_twenty_seed_protocol(self):
        screening = load_document(
            CONFIG_ROOT / "bilateral_network_v3_screening.json"
        )
        locked = load_document(
            CONFIG_ROOT / "bilateral_network_v3_locked.json"
        )
        plan = build_trial_plan(locked)

        self.assertEqual(plan["split"], "locked")
        self.assertEqual(plan["pair_count"], 200)
        self.assertEqual(plan["trial_count"], 200)
        self.assertEqual(
            {trial["method"]["method_id"] for trial in plan["trials"]},
            {"proposed_energy"},
        )
        self.assertNotEqual(
            screening["factors"]["bilateral_data_root_seed"][0],
            locked["factors"]["bilateral_data_root_seed"][0],
        )
        locked_metrics = load_document(
            CONFIG_ROOT / "metrics_bilateral_network_v3_locked.json"
        )
        screening_metrics = load_document(
            CONFIG_ROOT
            / "metrics_bilateral_network_v3_screening.json"
        )
        self.assertEqual(
            locked_metrics["network"]["paired_gates"],
            screening_metrics["network"]["paired_gates"],
        )


if __name__ == "__main__":
    unittest.main()
