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

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

#[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>) -> impl Responder {
    let area = Area::read_by_id(pool.get_ref(), user.area_id)
        .await
        .unwrap();

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

    let template = ProfileTemplate { user };

    return template.to_response();
}
