use actix_web::{web, HttpResponse, Responder};
use chrono::NaiveDate;
use rinja::Template;
use serde::Deserialize;

use crate::endpoints::availability::NewOrEditAvailabilityTemplate;
use crate::models::User;
use crate::utils::ApplicationError;

#[derive(Deserialize)]
struct AvailabilityNewQuery {
    #[serde(rename(deserialize = "wholeday"))]
    whole_day: Option<bool>,
    date: NaiveDate,
}

#[actix_web::get("/availabillity/new")]
pub async fn get(
    user: web::ReqData<User>,
    query: web::Query<AvailabilityNewQuery>,
) -> Result<impl Responder, ApplicationError> {
    let template = NewOrEditAvailabilityTemplate {
        user: user.into_inner(),
        date: query.date,
        whole_day: query.whole_day.unwrap_or(true),
        id: None,
        start_time: None,
        end_time: None,
        comment: None,
    };

    Ok(HttpResponse::Ok().body(template.render()?))
}
