import plotly.graph_objects as go


def load_data():
    """Return illustrative daily ticket counts for two service desks."""
    return [
        (1, 22, 28),
        (2, 26, 32),
        (3, 30, 35),
        (4, 40, 44),
        (5, 46, 42),
        (6, 48, 40),
        (7, 52, 43),
        (8, 55, 44),
    ]


def value_table(data):
    """Return both service desks' resolved ticket counts by day."""
    return {
        "columns": ["Day", "North desk", "South desk"],
        "rows": [list(row) for row in data],
    }


def _ticket_chart(data) -> go.Figure:
    days, north, south = zip(*data, strict=True)
    fig = go.Figure()
    for name, counts, color in (
        ("North desk", north, "#245785"),
        ("South desk", south, "#9c4221"),
    ):
        fig.add_scatter(x=days, y=counts, name=name, mode="lines+markers", line_color=color)
    fig.update_layout(
        template="simple_white",
        font={"family": "Inter, system-ui, -apple-system, sans-serif", "color": "#1f2937"},
        title={"text": "Tickets resolved by service desk", "x": 0, "xanchor": "left"},
        xaxis={"title": "Day", "tickvals": days, "range": [0.75, 8.25]},
        yaxis={"title": "Tickets resolved", "range": [20, 60]},
        margin={"l": 72, "r": 96, "t": 56, "b": 60},
    )
    return fig


def make_after(data) -> go.Figure:
    """Label both lines in the right margin at their final values."""
    fig = _ticket_chart(data)
    fig.update_layout(showlegend=False)
    for trace in fig.data:
        fig.add_annotation(
            x=1,
            xref="paper",
            y=trace.y[-1],
            yref="y",
            xanchor="left",
            xshift=4,
            text=trace.name,
            showarrow=False,
            font={"color": trace.line.color},
        )
    return fig


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