import plotly.graph_objects as go


import plotly.io as pio


INK = "#1C1917"


MUTED = "#57534E"


WARM_EDITORIAL_PALETTE = [
    "#0F766E",
    "#C2410C",
    "#1D4ED8",
    "#A16207",
    "#7E22CE",
    "#475569",
    "#0369A1",
    "#1C1917",
]


CANVAS = "#FFF7ED"


RULE = "#E7DDD2"


pio.templates["warm_editorial"] = go.layout.Template(
    layout=go.Layout(
        colorway=WARM_EDITORIAL_PALETTE,
        font={"family": "Inter, system-ui, -apple-system, sans-serif", "size": 13, "color": INK},
        title={
            "x": 0,
            "xanchor": "left",
            "font": {"family": "Georgia, 'Times New Roman', serif", "size": 18, "color": INK},
        },
        paper_bgcolor=CANVAS,
        plot_bgcolor=CANVAS,
        xaxis={
            "showgrid": False,
            "showline": True,
            "linecolor": "#A8A29E",
            "tickcolor": "#A8A29E",
            "tickfont": {"color": MUTED},
            "automargin": True,
        },
        yaxis={
            "showgrid": True,
            "gridcolor": RULE,
            "gridwidth": 1,
            "showline": False,
            "tickcolor": "#A8A29E",
            "tickfont": {"color": MUTED},
            "zeroline": False,
            "automargin": True,
        },
    )
)


def load_data():
    return [
        ("Login", "Access", 20, 16),
        ("Password", "Access", 12, 14),
        ("Network", "Connection", 15, 13),
        ("Wi-Fi", "Connection", 9, 8),
        ("Scanner", "Equipment", 0, 3),
        ("Printing", "Equipment", None, 4),
    ]


def make_after(data):
    from plotly.subplots import make_subplots

    fig = make_subplots(
        rows=1, cols=2, shared_yaxes=True, subplot_titles=("Last month", "This month")
    )
    rows = [f"{group} · {tag}" for tag, group, _, _ in data]
    for col, field in [(1, 3), (2, 2)]:
        for index, (_, _, current, previous) in enumerate(data):
            value = previous if field == 3 else current
            if value is None:
                fig.add_annotation(
                    x=0,
                    y=rows[index],
                    text="Not recorded",
                    showarrow=False,
                    xanchor="left",
                    row=1,
                    col=col,
                )
            else:
                fig.add_trace(
                    go.Bar(
                        x=[value],
                        y=[rows[index]],
                        orientation="h",
                        marker_color=WARM_EDITORIAL_PALETTE[0],
                        text=[f"{value}%"],
                        textposition="outside",
                        cliponaxis=False,
                    ),
                    row=1,
                    col=col,
                )
    fig.update_yaxes(categoryorder="array", categoryarray=rows, autorange="reversed")
    fig.update_xaxes(title_text="Share of 100 tickets (%)", range=[0, 26], dtick=10)
    fig.update_layout(
        template="warm_editorial",
        title_text="A ticket can carry several issue tags",
        showlegend=False,
        margin=dict(t=88),
    )
    return fig


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