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

use crate::{models::{Event, Role, User, Vehicle}, utils::ApplicationError};

pub struct VehicleAssignmentQuery {
    event: i32,
    vehicle: i32
}

#[actix_web::post("/vehicle-assignment")]
pub async fn post_new(
    user: web::ReqData<User>,
    pool: web::Data<PgPool>,
    query: web::Query<VehicleAssignmentQuery>
) -> Result<impl Responder, ApplicationError> {
    let Some(event) = Event::read_by_id_including_location(pool.get_ref(), query.event).await?
    else {
        return Ok(HttpResponse::NotFound().finish());
    };

    let user_is_admin_or_area_manager_of_event_area = user.role == Role::Admin
        || (user.role == Role::AreaManager
            && user.area_id == event.location.as_ref().unwrap().area_id);

    if !user_is_admin_or_area_manager_of_event_area {
        return Err(ApplicationError::Unauthorized);
    }

    let Some(vehicle) = Vehicle::read(pool.get_ref(), query.vehicle).await? else {
        return Ok(HttpResponse::NotFound().finish());
    };



}
