import plotly.graph_objects as go


def load_data():
    """Return illustrative daily water use at two greenhouse sites in liters."""
    return [
        (1, 320, 420),
        (2, 350, 440),
        (3, 410, 460),
        (4, 490, 450),
        (5, 530, 430),
        (6, 470, 390),
        (7, 380, 410),
    ]


def value_table(data):
    """Return both greenhouse sites' daily water use in liters."""
    return {
        "columns": ["Day", "Hill site (L)", "Valley site (L)"],
        "rows": [list(row) for row in data],
    }


def _water_use_chart(data) -> go.Figure:
    days, hill, valley = zip(*data, strict=True)
    fig = go.Figure(
        [
            go.Scatter(x=days, y=hill, name="Hill site", mode="lines", line_color="#245785"),
            go.Scatter(x=days, y=valley, name="Valley site", mode="lines", line_color="#9c4221"),
        ]
    )
    fig.update_layout(
        template="simple_white",
        font={"family": "Inter, system-ui, -apple-system, sans-serif", "color": "#1f2937"},
        title={"text": "Daily greenhouse water use", "x": 0, "xanchor": "left"},
        xaxis={"title": "Day", "tickvals": days, "range": [0.8, 7.2]},
        yaxis={"title": "Water use (L)", "range": [0, 560]},
        margin={"l": 76, "r": 100, "t": 56, "b": 60},
    )
    return fig


def make_after(data) -> go.Figure:
    """Use distinct line styles and endpoint labels to keep both series readable."""
    fig = _water_use_chart(data)
    fig.update_layout(showlegend=False, yaxis_range=[280, 560])
    fig.data[1].update(line_dash="dash")
    for trace in fig.data:
        fig.add_annotation(
            x=1,
            xref="paper",
            y=trace.y[-1],
            yref="y",
            xanchor="left",
            text=trace.name,
            showarrow=False,
            font={"color": trace.line.color},
        )
    return fig


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