//! Rhai wrapper for KubernetesContext

use crate::kubernetes::KubernetesContext;
use rhai::{EvalAltResult, Array, Dynamic};
use super::runtime::{get_runtime, to_rhai_err};
use super::namespace::NamespaceRhai;

/// Create a global Kubernetes context
pub fn kubernetes() -> Result<KubernetesContextRhai, Box<EvalAltResult>> {
    Ok(KubernetesContextRhai)
}

/// Rhai wrapper for KubernetesContext
#[derive(Clone)]
pub struct KubernetesContextRhai;

impl KubernetesContextRhai {
    /// Reset the Kubernetes cluster
    pub fn reset(&self) -> Result<(), Box<EvalAltResult>> {
        let rt = get_runtime();
        rt.block_on(async {
            let mut ctx = KubernetesContext::new()
                .await
                .map_err(|e| format!("Context error: {}", e))?;
            ctx.reset()
                .await
                .map_err(|e| format!("Reset error: {}", e))
        })
        .map_err(|e| to_rhai_err(e))
    }

    /// Get markdown representation of cluster state
    pub fn md(&self) -> Result<String, Box<EvalAltResult>> {
        let rt = get_runtime();
        rt.block_on(async {
            let ctx = KubernetesContext::new()
                .await
                .map_err(|e| format!("Context error: {}", e))?;
            ctx.md().await.map_err(|e| format!("MD error: {}", e))
        })
        .map_err(|e| to_rhai_err(e))
    }

    /// List all namespaces
    pub fn ns_list(&self) -> Result<Array, Box<EvalAltResult>> {
        let rt = get_runtime();
        let list = rt.block_on(async {
            let ctx = KubernetesContext::new()
                .await
                .map_err(|e| format!("Context error: {}", e))?;
            ctx.ns_list().await.map_err(|e| format!("List error: {}", e))
        })
        .map_err(|e| to_rhai_err(e))?;
        
        // Convert Vec<String> to Rhai Array
        Ok(list.into_iter().map(Dynamic::from).collect())
    }

    /// Delete a namespace
    pub fn ns_delete(&self, name: &str) -> Result<(), Box<EvalAltResult>> {
        let name = name.to_string();
        let rt = get_runtime();
        rt.block_on(async {
            let ctx = KubernetesContext::new()
                .await
                .map_err(|e| format!("Context error: {}", e))?;
            ctx.ns_delete(&name)
                .await
                .map_err(|e| format!("Delete error: {}", e))
        })
        .map_err(|e| to_rhai_err(e))
    }

    /// Get or create a namespace
    pub fn namespace(&self, name: &str) -> Result<NamespaceRhai, Box<EvalAltResult>> {
        let name = name.to_string();
        let rt = get_runtime();
        rt.block_on(async {
            let ctx = KubernetesContext::new()
                .await
                .map_err(|e| format!("Context error: {}", e))?;
            let ns = ctx
                .namespace(&name)
                .await
                .map_err(|e| format!("Namespace error: {}", e))?;
            Ok::<NamespaceRhai, String>(NamespaceRhai::new(ns))
        })
        .map_err(|e: String| to_rhai_err(e))
    }
}
