#![cfg(feature = "kubernetes")]

//! Integration tests for Registry Build and Push functionality
//!
//! These tests verify the complete workflow of building and pushing container images
//! to a Kubernetes-deployed registry.
//!
//! # Requirements
//!
//! - A running Kubernetes cluster
//! - kubectl configured and accessible
//! - buildah installed on the system
//! - The kubernetes feature enabled
//!
//! # Note
//!
//! These tests are designed to be run in an environment with Kubernetes access.
//! In CI/CD environments without a cluster, these will be skipped or require
//! special configuration.

#[cfg(all(feature = "kubernetes", test))]
mod tests {
    use herolib_virt::kubernetes::RegistryConfigBuilder;

    /// Test RegistryConfigBuilder defaults
    #[test]
    fn test_registry_config_defaults() {
        let config = RegistryConfigBuilder::new("test-registry").build();

        assert_eq!(config.image, "registry:2");
        assert_eq!(config.replicas, 1);
        assert_eq!(config.port, 5000);
        assert_eq!(config.node_port, 30500);
        assert_eq!(config.timeout_secs, 120);
        assert_eq!(config.reset, false);  // Default: reuse environments
    }

    /// Test RegistryConfigBuilder with custom settings
    #[test]
    fn test_registry_config_custom() {
        let config = RegistryConfigBuilder::new("my-registry")
            .node_port(30500)
            .timeout_secs(60)
            .build();

        assert_eq!(config.node_port, 30500);
        assert_eq!(config.timeout_secs, 60);
        assert_eq!(config.reset, false);  // Environments are reused by default
    }

    /// Test RegistryConfigBuilder with authentication
    #[test]
    fn test_registry_config_with_auth() {
        let config = RegistryConfigBuilder::new("secure-registry")
            .with_auth("admin", "password123")
            .build();

        assert_eq!(config.username, Some("admin".to_string()));
        assert_eq!(config.password, Some("password123".to_string()));
    }

    /// Test RegistryConfigBuilder with persistence
    #[test]
    fn test_registry_config_with_persistence() {
        let config = RegistryConfigBuilder::new("persistent-registry")
            .persistence(true, "50Gi")
            .build();

        assert_eq!(config.persistence_enabled, true);
        assert_eq!(config.persistence_size, "50Gi");
    }

    /// Test RegistryConfigBuilder reset option
    #[test]
    fn test_registry_config_reset() {
        let config = RegistryConfigBuilder::new("test-registry")
            .reset(true)
            .build();

        assert_eq!(config.reset, true);
    }

    /// Test RegistryConfigBuilder fluent API chaining
    #[test]
    fn test_registry_config_fluent_chaining() {
        let config = RegistryConfigBuilder::new("complex-registry")
            .node_port(30500)
            .with_auth("user", "pass")
            .persistence(true, "100Gi")
            .timeout_secs(120)
            .build();

        assert_eq!(config.node_port, 30500);
        assert_eq!(config.username, Some("user".to_string()));
        assert_eq!(config.password, Some("pass".to_string()));
        assert_eq!(config.persistence_enabled, true);
        assert_eq!(config.persistence_size, "100Gi");
        assert_eq!(config.timeout_secs, 120);
        assert_eq!(config.reset, false);  // Environments reused by default
    }

    /// Verify that the Kubernetes manager can be instantiated
    /// Note: This test may fail in CI/CD environments without actual Kubernetes access
    #[tokio::test]
    #[ignore] // Only run in environments with Kubernetes
    async fn test_kubernetes_manager_instantiation() {
        // This would require actual Kubernetes cluster access
        // Uncomment only in appropriate test environments
        // let km = KubernetesManager::new("test").await;
        // assert!(km.is_ok());
    }

    /// Verify that registry deployment configuration is valid
    #[test]
    fn test_registry_deployment_config_validation() {
        let config = RegistryConfigBuilder::new("validation-test")
            .node_port(30500)
            .timeout_secs(120)
            .build();

        // Validate configuration values
        assert!(config.node_port > 0);
        assert!(config.timeout_secs > 0);
        assert!(config.replicas > 0);
        assert!(!config.image.is_empty());
    }

    /// Test that registry config builder handles edge cases
    #[test]
    fn test_registry_config_edge_cases() {
        // Very large timeout
        let config = RegistryConfigBuilder::new("large-timeout")
            .timeout_secs(3600)
            .build();
        assert_eq!(config.timeout_secs, 3600);

        // Small node port
        let config = RegistryConfigBuilder::new("small-nodeport")
            .node_port(30000)
            .build();
        assert_eq!(config.node_port, 30000);

        // Large storage size
        let config = RegistryConfigBuilder::new("large-storage")
            .persistence(true, "1000Gi")
            .build();
        assert_eq!(config.persistence_size, "1000Gi");
    }
}
