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_before(data) -> go.Figure:
    fig = _repair_time_chart(data)
    fig.update_layout(title_font_size=16, margin={"l": 68, "r": 28, "t": 110, "b": 105})
    _add_repair_note(fig, "<b>Reopened tickets are excluded.</b>", 1.12, 16)
    _add_repair_note(fig, "<b>Field service, first half of 2026</b>", -0.28, 16)
    return fig


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