#!/usr/bin/env python3
"""Offline explorer for a fictional five-row model, not a physical estimate."""

from __future__ import annotations

import argparse
from decimal import Decimal, InvalidOperation

RECORDED = (160, 80, 44, 26, 20)


def report(factor: Decimal) -> str:
    if not factor.is_finite() or not (Decimal(0) < factor <= Decimal(1)):
        raise ValueError("The conditional factor must be greater than 0 and at most 1.")
    rows = []
    gaps = []
    for n, value in enumerate(RECORDED):
        prediction = Decimal(160) * factor**n
        gap = abs(Decimal(value) - prediction)
        gaps.append(gap)
        rows.append(f"{n:>2} | {prediction:>13.4f} | {value:>8} | {gap:>12.4f}")
    mean = sum(gaps) / Decimal(len(gaps))
    return "\n".join(
        [
            "P(n) = 160 x r^n; ALL record values are INVENTED arbitrary units",
            f"chosen r = {factor}",
            " n | model prediction | invented | absolute gap",
            *rows,
            f"Mean absolute gap over these five rows: {mean:.4f}",
            "A smaller gap is fit to this fictional table, not proof of a mechanism or real safety.",
        ]
    )


def main() -> int:
    parser = argparse.ArgumentParser(description=__doc__)
    parser.add_argument("--factor", help="A proposed per-layer factor in (0, 1], e.g. .50")
    args = parser.parse_args()
    raw = args.factor if args.factor is not None else input("Choose factor r (0 < r <= 1): ").strip()
    try:
        print(report(Decimal(raw)))
    except (InvalidOperation, ValueError) as exc:
        parser.error(str(exc))
    return 0


if __name__ == "__main__":
    raise SystemExit(main())
