"""Report how often a center's past forecasts diverge from the current guidance.

    python -m cli.audit --center sac
"""

import typer

from app.audit import audit

from .common import center_by_slug, connect

app = typer.Typer()


@app.command()
def main(center: str = typer.Option(...), era: str = typer.Option(None)):
    with connect() as conn:
        r = audit(conn, center_by_slug(conn, center)["id"], era)
    n = r["forecasts"]
    typer.echo(f"{n} forecasts; any divergence: {r['any']} ({r['any'] / n:.0%}); "
               f"excluding bottom-line length: {r['substantive']} ({r['substantive'] / n:.0%})\n")
    for c in r["checks"]:
        typer.echo(f"{c['count']:>5}  {c['share']:>6.1%}  {c['label']}  [{c['source']}]")
    typer.echo(f"\npairs listed: {r['pairs']}\n")
    typer.echo("season   forecasts  any   substantive  normal_caution  pairing  rated_w/o_problem  bottom_line_long")
    for s, c in r["by_season"].items():
        f = c["forecasts"]
        typer.echo(f"{s:<8} {f:>9}  {c.get('any', 0) / f:>4.0%}  {c.get('substantive', 0) / f:>11.0%}  "
                   f"{c.get('normal_caution', 0) / f:>14.0%}  {c.get('discouraged_pairing', 0) / f:>7.0%}  "
                   f"{c.get('rated_without_problem', 0) / f:>17.0%}  {c.get('bottom_line_long', 0) / f:>16.0%}")


if __name__ == "__main__":
    app()
