"""Exact continuous-phase illustration, not an HDD benchmark."""
from fractions import Fraction
import json
from pathlib import Path

def waits(positions, sectors=12):
    """Return mean first/last-copy encounter waits in sector-time units."""
    if type(sectors) is not int or sectors < 1:
        raise ValueError("sectors must be a positive integer")
    positions = tuple(positions)
    if not positions or any(type(p) is not int or not 0 <= p < sectors for p in positions):
        raise ValueError("positions must be nonempty integer coordinates within the track")
    if len(set(positions)) != len(positions):
        raise ValueError("copies must occupy distinct positions")
    points = sorted(positions)
    gaps = [b - a for a, b in zip(points, points[1:] + [points[0] + sectors])]
    first = Fraction(sum(g * g for g in gaps), 2 * sectors)
    return first, sectors - first

def mixed_wait(positions, read_fraction, sectors=12):
    if not isinstance(read_fraction, (int, Fraction)) or isinstance(read_fraction, bool):
        raise ValueError("read fraction must be exact")
    if not 0 <= read_fraction <= 1:
        raise ValueError("read fraction must lie in [0, 1]")
    read, write = waits(positions, sectors)
    return read_fraction * read + (1 - read_fraction) * write

def results():
    period_ms = Fraction(60000, 7200)
    cases = [
        ("One copy", [0]),
        ("Two evenly spaced", [0, 6]),
        ("Two clustered", [0, 1]),
        ("Three evenly spaced", [0, 4, 8]),
        ("Four evenly spaced", [0, 3, 6, 9]),
    ]
    rows = []
    for label, positions in cases:
        read, write = waits(positions)
        rows.append({
            "layout": label, "positions": positions, "copies": len(positions),
            "read_mean_turns": str(read / 12),
            "write_all_mean_turns": str(write / 12),
            "read_mean_ms": round(float(read / 12 * period_ms), 4),
            "write_all_mean_ms": round(float(write / 12 * period_ms), 4),
            "equal_mix_mean_ms": round(float(mixed_wait(positions, Fraction(1, 2)) / 12 * period_ms), 4),
            "copy_payload_fraction": str(Fraction(1, len(positions))),
        })
    return {
        "schema_version": 1, "kind": "constructed-analytical-illustration",
        "generated_date": "2026-11-10", "rpm": 7200, "phase_coordinates_per_turn": 12,
        "phase_distribution": "continuous uniform after positioning",
        "excluded_costs": ["seek", "transfer duration", "track switches", "queueing", "controller", "replica consistency protocol"],
        "write_event": "encounter the last copy position in one forward sweep, not durable completion",
        "rows": rows,
    }

if __name__ == "__main__":
    path = Path(__file__).with_name("results.json")
    path.write_text(json.dumps(results(), indent=2) + "\n")
    print(path)
