use actix_identity::Identity;
use actix_web::{http::header::LOCATION, HttpResponse, Responder};
use rinja::Template;

use crate::utils::ApplicationError;

#[derive(Template)]
#[template(path = "user/login.html")]
struct LoginTemplate {}

#[actix_web::get("/login")]
async fn get(user: Option<Identity>) -> Result<impl Responder, ApplicationError> {
    if let Some(_) = user {
        Ok(HttpResponse::Found()
            .insert_header((LOCATION, "/"))
            .finish())
    } else {
        let template = LoginTemplate {};

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