//! Per-call scoping context shared by every harness layer.

use crate::config::{PermissionConfig, SandboxConfig};
use crate::permission::check::PermissionContext;
use crate::tool::{ClientKind, ProtocolKind};
use std::path::PathBuf;
use std::sync::Arc;

/// Per-call scoping context. Passed to every layer below the registry.
///
/// `connector_id` and `session_id` mirror what the existing
/// [`PermissionContext`] uses; `client_kind` and `protocol` are the new
/// override surface for per-client / per-protocol behaviour.
#[derive(Clone)]
pub struct ToolContext {
    pub connector_id: Arc<str>,
    pub session_id: Option<Arc<str>>,
    pub client_kind: ClientKind,
    pub protocol: ProtocolKind,
    pub workspace_root: PathBuf,
    pub sandbox: Arc<SandboxConfig>,
    pub permission: Arc<PermissionConfig>,
    pub permission_context: Arc<PermissionContext>,
}

impl ToolContext {
    /// Test/builder helper. Real callers compose this from connector state.
    pub fn for_test(
        connector_id: impl Into<Arc<str>>,
        client_kind: ClientKind,
        protocol: ProtocolKind,
        workspace_root: PathBuf,
        sandbox: SandboxConfig,
        permission: PermissionConfig,
        permission_context: PermissionContext,
    ) -> Self {
        Self {
            connector_id: connector_id.into(),
            session_id: None,
            client_kind,
            protocol,
            workspace_root,
            sandbox: Arc::new(sandbox),
            permission: Arc::new(permission),
            permission_context: Arc::new(permission_context),
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::config::WhitelistConfig;
    use crate::permission::whitelist::CompiledWhitelist;

    #[test]
    fn context_builds_with_test_helper() {
        let whitelist = CompiledWhitelist::compile(&WhitelistConfig::default()).unwrap();
        let perm_ctx = PermissionContext::new(
            "conn-1".to_string(),
            None,
            whitelist,
        );
        let ctx = ToolContext::for_test(
            "conn-1",
            ClientKind::claude(),
            ProtocolKind::acp(),
            PathBuf::from("/tmp"),
            SandboxConfig::default(),
            PermissionConfig::default(),
            perm_ctx,
        );
        assert_eq!(&*ctx.connector_id, "conn-1");
        assert_eq!(ctx.client_kind, ClientKind::claude());
    }
}
