import plotly.graph_objects as go


def load_data():
    return [
        ("Ash", 120, 108),
        ("Birch", 180, 172),
        ("Cedar", 150, 128),
        ("Elm", 160, 154),
        ("Pine", 200, 190),
    ]


def value_table(data):
    return {
        "columns": ["Plant", "Target output (units)", "Actual output (units)"],
        "rows": [list(row) for row in data],
    }


def _output_chart_layout(fig, title: str, axis_title: str, upper_limit: int) -> go.Figure:
    fig.update_layout(
        template="simple_white",
        font={"family": "Inter, system-ui, -apple-system, sans-serif", "color": "#1f2937"},
        title={"text": title, "x": 0.08},
        xaxis={"title": axis_title, "range": [0, upper_limit]},
        yaxis={"autorange": "reversed"},
        margin={"l": 78, "r": 30, "t": 90, "b": 62},
    )
    return fig


def make_before(data) -> go.Figure:
    plants, targets, actuals = zip(*data, strict=True)
    fig = go.Figure(
        [
            go.Bar(x=targets, y=plants, orientation="h", name="Target", marker_color="#919faa"),
            go.Bar(x=actuals, y=plants, orientation="h", name="Actual", marker_color="#246487"),
        ]
    )
    fig.update_layout(barmode="group", legend={"orientation": "h", "x": 0, "y": 1.02})
    return _output_chart_layout(fig, "Production targets and actual output", "Units", 220)


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