package data

import (
	"context"
	"database/sql"
	"time"
)

type PredictPoint struct {
	ForecastTime time.Time
	RainMMx1000  int32
}

// ForecastRainAtIssued returns forecast hourly rows for a given station/provider at an exact issued_at time.
func ForecastRainAtIssued(ctx context.Context, stationID, provider string, issuedAt time.Time) ([]PredictPoint, error) {
	const q = `
        SELECT forecast_time, rain_mm_x1000
        FROM forecast_hourly
        WHERE station_id=$1 AND provider=$2 AND issued_at=$3
        ORDER BY forecast_time ASC`
	rows, err := DB().QueryContext(ctx, q, stationID, provider, issuedAt)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	var out []PredictPoint
	for rows.Next() {
		var p PredictPoint
		var rain sql.NullInt32
		if err := rows.Scan(&p.ForecastTime, &rain); err != nil {
			continue
		}
		if rain.Valid {
			p.RainMMx1000 = rain.Int32
		}
		out = append(out, p)
	}
	return out, nil
}
