import plotly.graph_objects as go


FONT = {"family": "Inter, system-ui, -apple-system, sans-serif", "color": "#1f2937"}


def load_data():
    return [
        ("Jan", 84, 78, 10),
        ("Feb", 92, 83, 12),
        ("Mar", 86, 91, 11),
        ("Apr", 105, 89, 16),
        ("May", 97, 102, 14),
        ("Jun", 89, 98, 9),
    ]


def value_table(data):
    return {
        "columns": ["Month", "Tickets opened", "Tickets closed", "Median wait (hours)"],
        "rows": [list(row) for row in data],
    }


def _support_traces(data, wait_axis):
    months, opened, closed, wait = zip(*data, strict=True)
    fig = go.Figure()
    fig.add_scatter(
        x=months, y=opened, name="Opened", mode="lines+markers", line={"color": "#24688c"}
    )
    fig.add_scatter(
        x=months, y=closed, name="Closed", mode="lines+markers", line={"color": "#8a4c22"}
    )
    fig.add_scatter(
        x=months,
        y=wait,
        yaxis=wait_axis,
        name="Median wait",
        mode="lines+markers",
        line={"color": "#596779"},
    )
    return fig


def make_after(data) -> go.Figure:
    fig = _support_traces(data, "y2")
    fig.update_layout(
        template="simple_white",
        font=FONT,
        showlegend=False,
        title="Support desk activity and response time",
        xaxis={"anchor": "y2", "type": "category", "range": [-0.15, 6.05]},
        yaxis={"title": "Tickets", "domain": [0.57, 1], "range": [70, 110]},
        yaxis2={"title": "Median wait (hours)", "domain": [0, 0.41], "range": [7, 17]},
        height=440,
        margin={"l": 82, "r": 30, "t": 60, "b": 48},
    )
    month, opened, closed, _ = data[-1]
    for label, value, color in (("Opened", opened, "#24688c"), ("Closed", closed, "#8a4c22")):
        fig.add_annotation(
            x=month,
            y=value,
            xshift=8,
            text=label,
            xanchor="left",
            showarrow=False,
            font={"color": color},
        )
    return fig


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