use actix_web::{web, HttpResponse, Responder};
use rinja::Template;

use crate::{
    endpoints::vehicle::VehicleNewOrEditTemplate,
    models::{Role, User},
    utils::ApplicationError,
};

#[actix_web::get("/vehicles/new")]
pub async fn get(user: web::ReqData<User>) -> Result<impl Responder, ApplicationError> {
    if user.role != Role::Admin {
        return Err(ApplicationError::Unauthorized);
    }

    let template = VehicleNewOrEditTemplate {
        user: user.into_inner(),
        vehicle: None,
    };

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