#!/usr/bin/env python3
"""Render the preregistered fidelity-envelope figure prototype."""

from pathlib import Path

import matplotlib.pyplot as plt
import numpy as np


OUT = Path(__file__).with_name("mock-fidelity-envelope.png")


def main() -> None:
    workloads = ["F0\n2048/1", "F1\n512/1", "F2\n2048/128", "T1\nexact trace"]
    variants = ["A0 analytical", "A1 native measured", "A2 measured+fix", "A3 +batch profile"]
    # Prototype values are deliberately marked as MOCK and encode possible,
    # distinguishable outcomes only. They are never read by result analysis.
    regret = np.array(
        [
            [12.5, 14.0, 18.0, 25.0],
            [11.0, 13.0, 16.0, 22.0],
            [7.5, 10.0, 12.0, 18.0],
            [2.0, 4.0, 7.0, 12.0],
        ]
    )

    fig, (ax0, ax1) = plt.subplots(
        1, 2, figsize=(11.5, 4.5), gridspec_kw={"width_ratios": [1.35, 1.0]}
    )
    x = np.arange(len(workloads))
    width = 0.19
    colors = ["#7f7f7f", "#4c78a8", "#f58518", "#54a24b"]
    for index, (variant, color) in enumerate(zip(variants, colors)):
        ax0.bar(
            x + (index - 1.5) * width,
            regret[index],
            width,
            label=variant,
            color=color,
        )
    ax0.axhline(5.0, color="#d62728", linestyle="--", linewidth=1.5, label="5% gate")
    ax0.set_xticks(x, workloads)
    ax0.set_ylabel("Worst selected-config regret (%)")
    ax0.set_title("A. Rank fidelity across workload complexity")
    ax0.legend(fontsize=8, ncol=2, frameon=False)
    ax0.grid(axis="y", alpha=0.25)

    consumption = np.array(
        [
            [0, 0, 0],
            [35, 65, 0],
            [100, 0, 0],
            [100, 0, 100],
        ]
    )
    bottom = np.zeros(len(variants))
    labels = ["measured collective hit", "analytical fallback", "batch-profile coverage"]
    stack_colors = ["#4c78a8", "#e45756", "#54a24b"]
    for values, label, color in zip(consumption.T, labels, stack_colors):
        ax1.barh(variants, values, left=bottom, label=label, color=color)
        bottom += values
    ax1.set_xlim(0, 200)
    ax1.set_xlabel("Coverage counters (normalized; separate axes by mechanism)")
    ax1.set_title("B. Profile consumption, not just final rank")
    ax1.grid(axis="x", alpha=0.25)
    ax1.legend(fontsize=8, frameon=False, loc="lower right")

    fig.suptitle("MOCK / preregistered layout — values are not experimental results", fontsize=12)
    fig.tight_layout()
    fig.savefig(OUT, dpi=180, bbox_inches="tight")
    print(OUT)


if __name__ == "__main__":
    main()
