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

use crate::{
    filters,
    models::{Area, Role, User},
    utils::ApplicationError,
};

#[derive(Template)]
#[template(path = "user/profile.html")]
struct ProfileTemplate {
    user: User,
}

#[actix_web::get("/profile")]
pub async fn get(
    user: web::ReqData<User>,
    pool: web::Data<PgPool>,
) -> Result<impl Responder, ApplicationError> {
    let area = Area::read_by_id(pool.get_ref(), user.area_id).await?;

    let mut user = user.into_inner();
    user.area = area;

    let template = ProfileTemplate { user };

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