import plotly.graph_objects as go


TITLE = "Distance and courier time"


COLUMNS = ["Area", "Minutes per km", "Observed minimum (km)", "Observed maximum (km)"]


def load_data():
    return [
        ("Central", 2.4, 1, 9),
        ("East", 1.8, 2, 14),
        ("North", 2.1, 1, 13),
        ("South", 1.6, 2, 17),
        ("West", 2.0, 1, 11),
    ]


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 make_after(data) -> go.Figure:
    rows = [(area, f"{low} to {high}", f"{slope * (7 - 2):.1f}") for area, slope, low, high in data]
    fig = go.Figure(
        table_trace(
            ["Area", "Observed range (km)", "Added time, 2 to 7 km (min)"],
            rows,
            ["left", "right", "right"],
            [1, 1.5, 2],
        )
    )
    return style_table(fig, TITLE)


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