use time::{Duration, OffsetDateTime};
use crate::domain::{GreenWindow, Region, WindowEstimator, greenest_window};
use crate::ports::ForecastModel;
use super::ApplicationError;
pub struct FindGreenestWindow<F: ForecastModel> {
forecast: F,
}
impl<F: ForecastModel> FindGreenestWindow<F> {
pub fn new(forecast: F) -> Self {
Self { forecast }
}
pub async fn execute(
&self,
region: Region,
methodology_id: &str,
from: OffsetDateTime,
horizon: Duration,
window: Duration,
estimator: WindowEstimator,
) -> Result<GreenWindow, ApplicationError> {
let points = self
.forecast
.forecast(region, methodology_id, from, horizon)
.await?;
greenest_window(&points, window, estimator).ok_or(ApplicationError::InsufficientSeries)
}
}