import plotly.graph_objects as go


def load_data():
    return [
        ("Jan", 38, 50),
        ("Feb", 40, 48),
        ("Mar", 35, 52),
        ("Apr", 43, 55),
        ("May", 45, 58),
        ("Jun", 42, 63),
    ]


def value_table(data):
    return {
        "columns": ["Month", "Walk-in repairs", "Scheduled repairs"],
        "rows": [list(row) for row in data],
    }


def _repair_panels(data, panel_titles) -> go.Figure:
    months, walk_in, scheduled = zip(*data, strict=True)
    fig = go.Figure()
    for index, (counts, title) in enumerate(zip((walk_in, scheduled), panel_titles, strict=True)):
        axis_suffix = "" if index == 0 else "2"
        fig.add_trace(
            go.Scatter(
                x=months,
                y=counts,
                mode="lines+markers",
                line={"color": "#246487"},
                xaxis=f"x{axis_suffix}",
                yaxis=f"y{axis_suffix}",
            )
        )
        fig.add_annotation(
            x=0 if index == 0 else 0.57,
            y=1.04,
            xref="paper",
            yref="paper",
            xanchor="left",
            yanchor="bottom",
            text=title,
            showarrow=False,
            font={"size": 15},
        )
    fig.update_layout(
        template="simple_white",
        font={"family": "Inter, system-ui, -apple-system, sans-serif", "color": "#1f2937"},
        title={"text": "Monthly bicycle repairs", "x": 0.08},
        xaxis={"domain": [0, 0.43], "anchor": "y", "tickangle": 0},
        xaxis2={"domain": [0.57, 1], "anchor": "y2", "matches": "x", "tickangle": 0},
        yaxis={"title": "Repairs", "range": [30, 70], "dtick": 10},
        yaxis2={"anchor": "x2", "matches": "y", "dtick": 10},
        showlegend=False,
        margin={"l": 65, "r": 30, "t": 108, "b": 52},
    )
    return fig


def make_after(data) -> go.Figure:
    return _repair_panels(data, ("<b>A. Walk-in repairs</b>", "<b>B. Scheduled repairs</b>"))


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