import plotly.express as px


import plotly.graph_objects as go


def load_data():
    frame = px.data.gapminder()
    nordics = ["Denmark", "Finland", "Iceland", "Norway", "Sweden"]
    latest = frame[(frame["year"] == 2007) & (frame["country"].isin(nordics))].sort_values(
        "lifeExp"
    )
    return [
        (str(country), round(float(life), 2))
        for country, life in latest[["country", "lifeExp"]].itertuples(index=False, name=None)
    ]


def value_table(data):
    return {"columns": ["Country", "Life expectancy (years)"], "rows": [list(row) for row in data]}


def make_after(data) -> go.Figure:
    countries, values = zip(*data, strict=True)
    colors = ["#0072B2" if c == "Iceland" else "#cbd5e1" for c in countries]
    fig = go.Figure(
        go.Bar(
            x=values,
            y=countries,
            orientation="h",
            marker={"color": colors},
            text=[f"{v:.1f}" for v in values],
            textposition="outside",
        )
    )
    fig.update_layout(
        template="simple_white",
        xaxis_title="Life expectancy (years)",
        xaxis_range=[0, 92],
        font={"family": "Inter, system-ui, -apple-system, sans-serif", "color": "#1f2937"},
    )
    return fig


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