// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: 2026 ndaal Gesellschaft für Sicherheit in der Informationstechnik mbH & Co KG, Cologne
// SPDX-FileCopyrightText: Author: Pierre Gronau <Pierre.Gronau@ndaal.eu>

//! Shared application state: the in-memory SARIF store, the export target
//! directory and process metadata.

use std::sync::Arc;
use std::time::Instant;

use crate::store::SarifStore;

/// Cheap-to-clone handle to the shared application state.
#[derive(Clone)]
pub struct AppState {
    inner: Arc<Inner>,
}

struct Inner {
    store: SarifStore,
    export_dir: String,
    listen: String,
    started: Instant,
}

impl AppState {
    /// Create the state with an export target directory and listen address.
    #[must_use]
    pub fn new(store: SarifStore, export_dir: String, listen: String) -> Self {
        Self {
            inner: Arc::new(Inner {
                store,
                export_dir,
                listen,
                started: Instant::now(),
            }),
        }
    }

    /// The shared SARIF store.
    #[must_use]
    pub fn store(&self) -> &SarifStore {
        &self.inner.store
    }

    /// The Export-menu target directory.
    #[must_use]
    pub fn export_dir(&self) -> &str {
        &self.inner.export_dir
    }

    /// The address the server listens on.
    #[must_use]
    pub fn listen(&self) -> &str {
        &self.inner.listen
    }

    /// Seconds since process start.
    #[must_use]
    pub fn uptime_secs(&self) -> u64 {
        self.inner.started.elapsed().as_secs()
    }
}
