use actix_web::{web, Responder};
use askama_actix::TemplateToResponse;
use sqlx::PgPool;

use crate::{
    endpoints::user::NewOrEditUserTemplate,
    models::{Area, Role, User},
};

#[actix_web::get("/users/new")]
pub async fn get_new(user: web::ReqData<User>, pool: web::Data<PgPool>) -> impl Responder {
    let mut areas: Option<Vec<Area>> = None;

    if user.role == Role::Admin {
        areas = Some(Area::read_all(pool.get_ref()).await.unwrap())
    }

    let template = NewOrEditUserTemplate {
        user: user.into_inner(),
        areas,
        id: None,
        email: None,
        name: None,
        role: None,
        function: None,
        area_id: None,
    };

    template.to_response()
}
