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 [
        (
            "Alder",
            [
                [0, 0, 1, 0, 0, 0, 2, 0, 0, 0],
                [0, 0, 0, 0, 0, 1, 0, 0, 0, 0],
                [0, 2, 0, 0, 0, 0, 0, 0, 0, 0],
            ],
        ),
        (
            "Birch",
            [
                [0, 4, 0, 0, 3, 0, 0, 5, 0, 0],
                [0, 0, 1, 0, 0, 0, 0, 0, 0, 0],
                [0, 0, 0, 2, 0, 0, 1, 0, 0, 0],
            ],
        ),
        (
            "Cedar",
            [
                [0, 0, 0, 2, 0, 0, 0, 0, 0, 0],
                [0, 2, 0, 0, 0, 0, 0, 3, 0, 0],
                [0, 0, 1, 0, 0, 0, 0, 0, 1, 0],
            ],
        ),
    ]


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

    fig = make_subplots(
        rows=1,
        cols=2,
        shared_yaxes=True,
        horizontal_spacing=0.1,
        column_widths=[0.6, 0.4],
        subplot_titles=("Flats needing repair", "Repair hours"),
    )
    labels, packed, totals = [], [], []
    for block, inspections in data:
        for check, values in zip(("Seal", "Latch", "Hinge"), inspections, strict=True):
            labels.append(f"{block} · {check}")
            count = sum(value > 0 for value in values)
            packed.append([1] * count + [0] * (len(values) - count))
            totals.append(sum(values))
    fig.add_trace(
        go.Heatmap(
            x=list(range(1, 11)),
            y=labels,
            z=packed,
            xgap=3,
            ygap=4,
            colorscale=[[0, RULE], [1, WARM_EDITORIAL_PALETTE[0]]],
            showscale=False,
        ),
        row=1,
        col=1,
    )
    fig.add_trace(
        go.Heatmap(
            x=["Total"],
            y=labels,
            z=[[v] for v in totals],
            text=[[str(v)] for v in totals],
            texttemplate="%{text}",
            ygap=4,
            colorscale=[[0, CANVAS], [1, WARM_EDITORIAL_PALETTE[0]]],
            zmin=0,
            showscale=False,
        ),
        row=1,
        col=2,
    )
    fig.update_yaxes(autorange="reversed")
    fig.update_xaxes(title_text="One teal tile per failed flat", tickvals=[1, 5, 10], row=1, col=1)
    fig.update_layout(
        template="warm_editorial",
        title_text="Birch seal repairs need twelve hours",
        height=460,
        margin=dict(t=88, b=58),
    )
    return fig


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