use rocket::{http::Status, serde::json::Json};

use crate::json_result::JsonResult;

use self::session::ensure_session;

pub mod create_user;
pub mod get_courses;
pub mod get_expiration_date;
pub mod register_course;
mod session;
pub mod update_expiration_date;
pub mod user;


/// Tries to connect to the online classroom, and gets a session cookie
/// if neccesary.
/// 
/// Mantains a global session cookie for ~20 minutes.
/// 
/// Every time a request is made the session cookie is checked. If more than
/// 20 minutes have passed, a new session cookie is requested.
#[get("/classroom/connect")]
pub async fn connection() -> (Status, Json<JsonResult<()>>) {
    match ensure_session().await {
        Ok(_) => (Status::Ok, JsonResult::ok(())),
        Err(err) => (Status::Ok, JsonResult::err(err)),
    }
}
