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

use crate::{endpoints::location::LocationTemplate, models::{Area, Role, User}};

#[actix_web::get("/locations/new")]
pub async fn get(user: web::ReqData<User>, pool: web::Data<PgPool>) -> impl Responder {
    if user.role == Role::AreaManager || user.role == Role::Admin {
        let mut areas = None;

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

        let template = LocationTemplate {
            user: user.into_inner(),
            areas,
            location: None
        };

        return template.to_response();
    }

    return HttpResponse::Unauthorized().finish();
}
