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_before(data) -> go.Figure:
    """Draw opaque fills so the Valley area hides parts of the Hill series."""
    fig = _water_use_chart(data)
    for trace in fig.data:
        trace.update(mode="none", fill="tozeroy", fillcolor=trace.line.color)
    return fig


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