import plotly.graph_objects as go


def load_data():
    exports = {
        "Indochina": (1969, 2352, 2248),
        "Morocco": (1934, 2585, 2829),
        "Tunisia": (1243, 1894, 2832),
        "Algeria": (6646, 8217, 11610),
    }
    return [
        (territory, year, count)
        for territory, counts in exports.items()
        for year, count in zip(range(1927, 1930), counts, strict=True)
    ]


def value_table(data):
    return {"columns": ["Territory", "Year", "Car exports"], "rows": [list(row) for row in data]}


def make_after(data) -> go.Figure:
    fig = go.Figure()
    colors = ("#0072B2", "#E69F00", "#009E73", "#CC79A7")
    for territory, color in zip(dict.fromkeys(row[0] for row in data), colors, strict=True):
        years, counts = zip(
            *((year, count) for name, year, count in data if name == territory), strict=True
        )
        fig.add_trace(go.Bar(x=years, y=counts, name=territory, marker_color=color))
    fig.update_layout(
        template="simple_white",
        barmode="group",
        yaxis_title="Car exports",
        xaxis_tickvals=[1927, 1928, 1929],
        legend={"orientation": "h", "y": 1.12},
    )
    fig.update_layout(
        font={"family": "Inter, system-ui, -apple-system, sans-serif", "color": "#1f2937"},
        colorway=["#3b82c4", "#d97706", "#14967f", "#8b5cf6", "#dc4c3e", "#78716c"],
    )
    return fig


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