//! Application state and context
//!
//! Extends the generic AppState from shared-api-lib with syntaxis-specific fields.

use shared_api_lib::auth::jwt::JwtProvider;
use std::sync::Arc;
use syntaxis_core::persistence::SqliteDatabase;

/// Project-lifecycle API state
///
/// Wraps the generic shared-api-lib::AppState and provides project-specific fields
/// while maintaining backward compatibility with code expecting Arc<> fields.
#[derive(Clone)]
pub struct AppState {
    /// Database persistence layer (syntaxis-core)
    pub db: Arc<SqliteDatabase>,

    /// Metrics collector (always present, may be empty)
    #[allow(dead_code)]
    pub metrics: Arc<crate::metrics::MetricsCollector>,

    /// Rate limiter for request throttling
    #[allow(dead_code)]
    pub rate_limiter: Arc<crate::rate_limit::RateLimiter>,

    /// Authentication provider for API key validation
    #[allow(dead_code)]
    pub auth: Arc<crate::auth::AuthProvider>,

    /// JWT token provider for JWT-based authentication
    /// Optional if JWT is not enabled in configuration
    pub jwt_provider: Option<Arc<JwtProvider>>,

    /// Event broadcaster for WebSocket real-time updates
    pub events: Arc<crate::websocket::EventBroadcaster>,
}

impl AppState {
    /// Create a new AppState with required fields
    #[allow(dead_code)]
    pub fn new(
        db: Arc<SqliteDatabase>,
        metrics: Arc<crate::metrics::MetricsCollector>,
        rate_limiter: Arc<crate::rate_limit::RateLimiter>,
        auth: Arc<crate::auth::AuthProvider>,
        jwt_provider: Option<Arc<JwtProvider>>,
        events: Arc<crate::websocket::EventBroadcaster>,
    ) -> Self {
        Self {
            db,
            metrics,
            rate_limiter,
            auth,
            jwt_provider,
            events,
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_app_state_creation() {
        // Verify the type compiles
        let _ = std::marker::PhantomData::<AppState>;
    }
}
