use super::api::prelude::Flash;
use leptos::*;

pub mod app;
pub mod flashbag;

#[server]
pub async fn get_flash(name: String) -> Result<Option<Flash>, ServerFnError<String>> {
    use super::api::prelude::*;

    // use crate::flashbag::FlashBag;
    let Some(session) = use_context::<FlashBag>() else {
        return Ok(None);
    };

    Ok(session.get(&name))
}

#[server]
pub async fn check_user() -> Result<Option<crate::domain::api::prelude::User>, ServerFnError<String>>
{
    use crate::domain::api::prelude::*;

    let app = use_context::<AppService>().unwrap();
    let session = use_context::<Session>().unwrap();

    Ok(app.get_user_session(&session).await)
}

#[server]
pub async fn check_forgot_password_token(
    token: Option<String>,
) -> Result<Option<crate::domain::api::prelude::User>, ServerFnError<String>> {
    use crate::domain::api::prelude::*;

    let app = use_context::<AppService>().unwrap();

    let Some(token) = token else { return Ok(None) };

    Ok(app.find_user_by_password_reset_token(&(token.into())).await)
}
