use std::sync::Arc;
use crate::config::AppConfig;
use anyhow::Result;
use common_contracts::persistence_client::PersistenceClient;
use dashmap::DashMap;
use uuid::Uuid;
use tokio::sync::Mutex;
use crate::workflow::WorkflowStateMachine;

pub struct AppState {
    pub config: AppConfig,
    pub persistence_client: PersistenceClient,
    // Key: request_id
    // We use Mutex here because state transitions need to be atomic per workflow
    pub workflows: Arc<DashMap<Uuid, Arc<Mutex<WorkflowStateMachine>>>>,
}

impl AppState {
    pub async fn new(config: AppConfig) -> Result<Self> {
        let persistence_client = PersistenceClient::new(config.data_persistence_service_url.clone());
        
        Ok(Self {
            config,
            persistence_client,
            workflows: Arc::new(DashMap::new()),
        })
    }
}

