use anyhow::Result;
use chrono::Local;
use rusqlite::Connection;

use crate::{db, model::event};

pub fn today(conn: &Connection) -> Result<String> {
    let today = Local::now().date_naive();
    let mut events = db::events::list_non_recurring_between(conn, today, today)?;
    let recurring_events = db::events::list_recurring(conn)?;
    let repetitions = event::repetitions_between(&recurring_events, today, today);
    for repetition in repetitions.values().flatten() {
        events.push(repetition.clone());
    }
    events.sort_by_key(|e| e.start);
    Ok(events
        .iter()
        .map(|e| format!("{}\n", e.pprint()))
        .collect::<Vec<String>>()
        .join(""))
}