use actix_web::{body::MessageBody, test::read_body, web, HttpResponse, Responder};
use askama_actix::TemplateToResponse;
use brass_macros::db_test;
use insta::assert_snapshot;

use crate::{
    endpoints::area::NewOrEditAreaTemplate,
    models::{Role, User},
    utils::test_helper::DbTestContext,
};

#[cfg(test)]
use crate::utils::test_helper::{test_get, RequestConfig};
#[cfg(test)]
use actix_http::StatusCode;

#[actix_web::get("/area/new")]
async fn get(user: web::ReqData<User>) -> impl Responder {
    if user.role != Role::Admin {
        return HttpResponse::Unauthorized().finish();
    }

    let template = NewOrEditAreaTemplate {
        user: user.into_inner(),
        area: None,
    };

    template.to_response()
}

#[db_test]
async fn produces_template(context: &DbTestContext) {
    let app = context.app().await;
    let config = RequestConfig {
        uri: "/area/new".to_string(),
        role: Role::Admin,
        function: crate::models::Function::Posten,
        user_area: 1,
    };
    let response = test_get(&context.db_pool, app, &config).await;

    assert_eq!(StatusCode::OK, response.status());

    let body = String::from_utf8(read_body(response).await.to_vec()).unwrap();
    assert_snapshot!(body);
}
