import plotly.graph_objects as go


def load_data():
    return [
        (10, 44),
        (15, 54),
        (20, 64),
        (25, 70),
        (30, 73),
        (35, 70),
        (40, 64),
    ]


def value_table(data):
    return {
        "columns": ["Reactor temperature (°C)", "Product yield (%)"],
        "rows": [list(row) for row in data],
    }


def yield_points(data):
    temperatures, yields = zip(*data, strict=True)
    return go.Scatter(
        x=temperatures,
        y=yields,
        mode="markers",
        marker={"color": "#1f2937", "size": 8},
        hovertemplate="%{x} °C: %{y}% measured<extra></extra>",
    )


def yield_layout(fig):
    fig.update_layout(
        template="simple_white",
        font={"family": "Inter, system-ui, -apple-system, sans-serif", "color": "#1f2937"},
        title="Product yield by reactor temperature",
        xaxis={"title": "Reactor temperature (°C)", "range": [7, 68]},
        yaxis={"title": "Product yield (%)", "range": [38, 90]},
        showlegend=False,
        margin={"l": 78, "r": 24, "t": 56, "b": 60},
    )
    return fig


def make_after(data) -> go.Figure:
    temperatures, yields = zip(*data, strict=True)
    fig = go.Figure(
        go.Scatter(
            x=temperatures,
            y=yields,
            mode="lines",
            line={"color": "#356890", "width": 2},
            hoverinfo="skip",
        )
    )
    fig.add_trace(yield_points(data))
    yield_layout(fig)
    fig.add_vrect(
        x0=temperatures[-1],
        x1=68,
        fillcolor="#e8edf2",
        line_width=0,
        layer="below",
    )
    fig.add_annotation(x=54, y=83, text="No measurements", showarrow=False)
    return fig


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