use std::sync::Arc;

use axum::extract::FromRef;
use leptos::get_configuration;

use crate::domain::api::ports::ApiService;

#[derive(Debug, Clone)]
/// The global application state shared between all request handlers.
pub struct AppState<S>
where
    S: ApiService,
{
    pub leptos_options: leptos::LeptosOptions,
    api_service: Arc<S>,
}

impl<S> AppState<S>
where
    S: ApiService,
{
    pub async fn new(api_service: S) -> Self {
        Self {
            leptos_options: get_configuration(None).await.unwrap().leptos_options,
            api_service: Arc::new(api_service),
        }
    }

    pub fn api_service(&self) -> Arc<S> {
        self.api_service.clone()
    }
}

impl<S> FromRef<AppState<S>> for leptos::LeptosOptions
where
    S: ApiService,
{
    fn from_ref(input: &AppState<S>) -> Self {
        input.leptos_options.clone()
    }
}
