"""Bounded, offline reproduction of non-finite paired-resampling results.

Run with the configured project interpreter from the repository root. This is
review evidence, not a regression test asserting that defective behavior is valid.
"""

import json
import math

from telemetry.performance.core.resampling import (
    PairedResamplingPlan,
    paired_resampling_interval,
)


def main() -> None:
    scenarios = (
        ("sum_overflow", (1e308, 1e308), (1e308, 1e308), False),
        ("difference_overflow", (-1e308, -1e308), (1e308, 1e308), True),
        ("ratio_overflow", (1e-308, 1e-308), (1e308, 1e308), True),
    )
    for name, baseline, candidate, use_first_value in scenarios:
        result = paired_resampling_interval(
            baseline,
            candidate,
            plan=PairedResamplingPlan(resamples=100),
            aggregate=(lambda values: values[0]) if use_first_value else None,
        )
        values = (
            result.baseline,
            result.candidate,
            result.delta,
            *result.confidence_interval,
            result.relative_change,
            *(result.relative_confidence_interval or ()),
        )
        print(
            json.dumps(
                {
                    "scenario": name,
                    "input_finite": all(math.isfinite(x) for x in (*baseline, *candidate)),
                    "point_estimates_finite": all(
                        math.isfinite(x) for x in (result.baseline, result.candidate)
                    ),
                    "nonfinite_result_fields": sum(
                        x is not None and not math.isfinite(x) for x in values
                    ),
                    "resamples": result.resamples,
                },
                sort_keys=True,
                allow_nan=False,
            )
        )


if __name__ == "__main__":
    main()
