use chrono::NaiveDate;
use serde::Serialize;
use sqlx::SqlitePool;

use self::{
    planned_event::{PlannedEvent, PlannedEventWithUserAndTriptype},
    trip::{Trip, TripWithUserAndType},
};

pub mod boat;
pub mod boatdamage;
pub mod boathouse;
pub mod boatreservation;
pub mod family;
pub mod location;
pub mod log;
pub mod logbook;
pub mod logtype;
pub mod mail;
pub mod notification;
pub mod planned_event;
pub mod role;
pub mod rower;
pub mod stat;
pub mod trip;
pub mod tripdetails;
pub mod triptype;
pub mod user;
pub mod usertrip;

#[derive(Serialize, Debug)]
pub struct Day {
    day: NaiveDate,
    planned_events: Vec<PlannedEventWithUserAndTriptype>,
    trips: Vec<TripWithUserAndType>,
    is_pinned: bool,
}

impl Day {
    pub async fn new(db: &SqlitePool, day: NaiveDate, is_pinned: bool) -> Self {
        if is_pinned {
            Self {
                day,
                planned_events: PlannedEvent::get_pinned_for_day(db, day).await,
                trips: Trip::get_pinned_for_day(db, day).await,
                is_pinned,
            }
        } else {
            Self {
                day,
                planned_events: PlannedEvent::get_for_day(db, day).await,
                trips: Trip::get_for_day(db, day).await,
                is_pinned,
            }
        }
    }
    pub async fn new_guest(db: &SqlitePool, day: NaiveDate, is_pinned: bool) -> Self {
        let mut day = Self::new(db, day, is_pinned).await;

        day.planned_events.retain(|e| e.planned_event.allow_guests);
        day.trips.retain(|t| t.trip.allow_guests);

        day
    }
}
