import plotly.graph_objects as go


def load_data():
    return [
        ("Jan", 22),
        ("Feb", 21),
        ("Mar", 20),
        ("Apr", 17),
        ("May", 18),
        ("Jun", 16),
    ]


def value_table(data):
    return {
        "columns": ["Month", "Median repair time (hours)"],
        "rows": [list(row) for row in data],
    }


def _repair_time_chart(data) -> go.Figure:
    periods, values = zip(*data, strict=True)
    fig = go.Figure(
        go.Scatter(x=periods, y=values, mode="lines+markers", line={"color": "#246487"})
    )
    fig.update_layout(
        template="simple_white",
        font={"family": "Inter, system-ui, -apple-system, sans-serif", "color": "#1f2937"},
        title={"text": "Equipment repair time", "x": 0.08},
        xaxis={"title": "Month", "tickmode": "array", "tickvals": list(periods), "tickangle": 0},
        yaxis_title="Median time (hours)",
        margin={"l": 68, "r": 28, "t": 80, "b": 65},
    )
    return fig


def _add_repair_note(fig, text, y, size):
    fig.add_annotation(
        x=0,
        y=y,
        xref="paper",
        yref="paper",
        xanchor="left",
        yanchor="middle",
        text=text,
        showarrow=False,
        font={"size": size},
    )


def make_after(data) -> go.Figure:
    fig = _repair_time_chart(data)
    fig.update_layout(
        title_font_size=21,
        title_subtitle={"text": "Field service, first half of 2026", "font": {"size": 14}},
        margin={"l": 68, "r": 28, "t": 88, "b": 105},
    )
    _add_repair_note(fig, "Reopened tickets are excluded.", -0.25, 12)
    return fig


if __name__ == "__main__":
    print(make_after(load_data()).to_json())
