from plotly.subplots import make_subplots


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 [
        ["Mon", 26, 30],
        ["Tue", 27, 30],
        ["Wed", 29, 30],
        ["Thu", 31, 30],
        ["Fri", 36, 30],
        ["Sat", 39, 30],
        ["Sun", 37, 30],
    ]


def value_table(data):
    return {"columns": ["Night", "Bed requests", "Available beds"], "rows": data}


def shelter_page(data, planning):
    titles = ("Beds requested", "Extra beds needed") if planning else ("Requests", "Beds available")
    fig = make_subplots(rows=1, cols=2, subplot_titles=titles, horizontal_spacing=0.15)
    days, requests, beds = zip(*data, strict=True)
    extra = [max(0, n - cap) for _, n, cap in data]
    color = WARM_EDITORIAL_PALETTE[0] if planning else "#BB1984"
    for column, counts in enumerate((requests, extra if planning else beds), 1):
        fig.add_bar(x=days, y=counts, row=1, col=column, marker_color=color)
    fig.update_layout(
        template="warm_editorial",
        showlegend=False,
        title_text="Plan for nine extra shelter beds on Saturday"
        if planning
        else "Shelter snapshot!",
        margin=dict(l=45, r=30, t=95, b=95),
        font_family="Inter, sans-serif" if planning else "Comic Sans MS, cursive",
    )
    fig.update_yaxes(rangemode="tozero")
    if planning:
        fig.add_hline(y=30, row=1, col=1, line_dash="dot", line_color=MUTED)
        fig.add_annotation(
            x="Tue", y=30, xref="x", yref="y", text="30 beds", showarrow=False, yanchor="bottom"
        )
        fig.add_annotation(
            x="Sat",
            y=max(extra),
            xref="x2",
            yref="y2",
            text="9 extra",
            showarrow=False,
            yanchor="bottom",
        )
        fig.update_yaxes(range=[0, 12], row=1, col=2)
        note = "Each request is for one bed. Extra beds cover requests above capacity.<br>Source: Illustrative shelter intake log, one winter week."
    else:
        note = "The shelter has thirty beds each night and the largest number of requests arrives on Saturday when thirty-nine people need a bed."
    fig.add_annotation(
        text=note,
        x=0,
        y=-0.16,
        xref="paper",
        yref="paper",
        xanchor="left",
        yanchor="top",
        font_size=13 if planning else 10,
        showarrow=False,
        align="left",
    )
    return fig


def make_after(data):
    return shelter_page(data, True)


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