import plotly.graph_objects as go


TITLE = "Warehouse inventory by region"


COLUMNS = ["Region", "Warehouse", "Units on hand", "Units reserved"]


def load_data():
    return [
        ("North", "Hill", 120, 20),
        ("North", "Lake", 98, 14),
        ("North", "Park", 105, 16),
        ("South", "Bay", 160, 26),
        ("South", "Cove", 142, 19),
        ("South", "Port", 136, 22),
    ]


def value_table(data):
    return {"columns": COLUMNS, "rows": [list(row) for row in data]}


def table_trace(columns, rows, align, widths, *, row_height=28, header_height=34, fill="#ffffff"):
    return go.Table(
        columnwidth=widths,
        header={
            "values": [f"<b>{column}</b>" for column in columns],
            "align": align,
            "height": header_height,
            "fill": {"color": "#eef2f6"},
            "line": {"width": 0},
            "font": {"color": "#1f2937", "size": 13},
        },
        cells={
            "values": list(zip(*rows, strict=True)),
            "align": align,
            "height": row_height,
            "fill": {"color": fill},
            "line": {"width": 0},
            "font": {"color": "#1f2937", "size": 13},
        },
    )


def style_table(fig, title, *, height=280, top=60, bottom=16):
    fig.update_layout(
        template="simple_white",
        font={"family": "Inter, system-ui, -apple-system, sans-serif", "color": "#1f2937"},
        title={"text": title, "x": 0.04, "xanchor": "left"},
        height=height,
        margin={"l": 24, "r": 24, "t": top, "b": bottom},
    )
    return fig


def table_note(fig, text, *, y=0, anchor="top", shift=-8):
    fig.add_annotation(
        text=text,
        x=0,
        y=y,
        xref="paper",
        yref="paper",
        xanchor="left",
        yanchor=anchor,
        yshift=shift,
        align="left",
        showarrow=False,
        font={"size": 12, "color": "#475569"},
    )


def inventory_pages(data, *, split, repeat_headings):
    align = ["left", "left", "right", "right"]
    widths = [1, 1, 1.4, 1.4]
    first = table_trace(COLUMNS, data[:split], align, widths)
    first.domain = {"y": [0.53, 0.94]}
    fig = go.Figure(first)
    continuation = data[split:]
    if repeat_headings:
        second = table_trace(COLUMNS, continuation, align, widths)
    else:
        rows = [
            ("", warehouse, on_hand, reserved) for _, warehouse, on_hand, reserved in continuation
        ]
        second = table_trace([""] * len(COLUMNS), rows, align, widths, header_height=0)
        second.header.fill.color = "#ffffff"
    second.domain = {"y": [0.02, 0.40]}
    fig.add_trace(second)
    table_note(fig, "Page 1", y=0.94, anchor="bottom", shift=4)
    table_note(fig, "Page 2", y=0.40, anchor="bottom", shift=4)
    fig.add_shape(
        type="line",
        x0=0,
        x1=1,
        y0=0.47,
        y1=0.47,
        xref="paper",
        yref="paper",
        line={"color": "#cbd5e1", "width": 1},
    )
    return style_table(fig, TITLE, height=420)


def make_before(data) -> go.Figure:
    return inventory_pages(data, split=4, repeat_headings=False)


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