#![allow(clippy::unwrap_used, clippy::expect_used, clippy::panic, clippy::needless_raw_string_hashes, clippy::duration_suboptimal_units, clippy::branches_sharing_code, clippy::used_underscore_binding, clippy::single_char_pattern, clippy::ignore_without_reason, clippy::cloned_ref_to_slice_refs, clippy::doc_overindented_list_items, clippy::match_wildcard_for_single_variants, clippy::ignored_unit_patterns, clippy::needless_collect, clippy::unnecessary_map_or, clippy::manual_flatten, clippy::manual_strip, clippy::future_not_send, clippy::unnested_or_patterns, clippy::no_effect_underscore_binding, clippy::literal_string_with_formatting_args)]
// DEAD: cmds::market removed; quarantined. References `ggen_cli_lib::cmds::market::*`,
// a module that no longer exists in crates/ggen-cli/src/cmds/mod.rs. Although this file
// is named in integration/mod.rs, that mod.rs is itself never compiled (no top-level
// tests/integration.rs does `mod integration;` and it is not a [[test]] target; it even
// references a non-existent `testcontainers_cleanroom` module). The `cfg(any())` guard
// below keeps this file inert even if accidentally included.
#![cfg(any())]

//! Marketplace Integration Tests with Testcontainers
//!
//! These tests validate production readiness of the marketplace using testcontainers
//! to create isolated test environments that mirror production conditions.

use assert_fs::prelude::*;
use ggen_cli_lib::cmds::market::{lockfile::*, registry::*};
use std::fs;
use tempfile::TempDir;

/// Helper to create a test registry with sample packages
fn create_test_registry() -> Registry {
    Registry {
        version: "1.0.0".to_string(),
        packages: vec![
            Package {
                name: "rig-mcp".to_string(),
                full_name: "rig-mcp-integration".to_string(),
                version: "0.1.0".to_string(),
                description: "Rig LLM framework with MCP protocol integration".to_string(),
                category: "ai".to_string(),
                author: "ggen-team".to_string(),
                repository: "https://github.com/ggen/rig-mcp".to_string(),
                path: "marketplace/packages/rig-mcp".to_string(),
                license: "MIT".to_string(),
                dependencies: vec![],
                features: vec![
                    "Multi-provider LLM support".to_string(),
                    "MCP protocol integration".to_string(),
                ],
                tags: vec!["ai".to_string(), "llm".to_string(), "mcp".to_string()],
                keywords: vec!["rig".to_string(), "mcp".to_string()],
            },
            Package {
                name: "api-endpoint".to_string(),
                full_name: "api-endpoint-templates".to_string(),
                version: "1.0.0".to_string(),
                description: "REST API endpoint templates with OpenAPI".to_string(),
                category: "templates".to_string(),
                author: "ggen-team".to_string(),
                repository: "https://github.com/ggen/api-endpoint".to_string(),
                path: "marketplace/packages/api-endpoint".to_string(),
                license: "MIT".to_string(),
                dependencies: vec![],
                features: vec!["Axum handlers".to_string(), "OpenAPI docs".to_string()],
                tags: vec!["api".to_string(), "rest".to_string()],
                keywords: vec!["api".to_string(), "endpoint".to_string()],
            },
        ],
    }
}

#[test]
fn test_registry_search_functionality() {
    let registry = create_test_registry();

    // Test exact name match
    let results = registry.search("rig-mcp", 10);
    assert_eq!(results.len(), 1);
    assert_eq!(results[0].name, "rig-mcp");

    // Test partial name match
    let results = registry.search("api", 10);
    assert_eq!(results.len(), 1);
    assert_eq!(results[0].name, "api-endpoint");

    // Test tag-based search
    let results = registry.search("llm", 10);
    assert_eq!(results.len(), 1);
    assert_eq!(results[0].name, "rig-mcp");

    // Test category search
    let ai_packages = registry.get_by_category("ai");
    assert_eq!(ai_packages.len(), 1);
    assert_eq!(ai_packages[0].name, "rig-mcp");
}

#[test]
fn test_lockfile_crud_operations() {
    let temp_dir = TempDir::new().unwrap();
    let lockfile_path = temp_dir.path().join("lock.json");

    // Create new lockfile
    let mut lockfile = Lockfile::new();
    assert_eq!(lockfile.version, "1.0.0");
    assert!(lockfile.packages.is_empty());

    // Add package
    let package = InstalledPackage {
        name: "rig-mcp".to_string(),
        full_name: "rig-mcp-integration".to_string(),
        version: "0.1.0".to_string(),
        checksum: "abc123def456".to_string(),
        source: "registry".to_string(),
        path: ".ggen/packages/rig-mcp".to_string(),
        installed_at: chrono::Utc::now().to_rfc3339(),
        dependencies: vec![],
    };

    lockfile.add_package(package.clone());
    assert_eq!(lockfile.packages.len(), 1);
    assert!(lockfile.has_package("rig-mcp"));

    // Save lockfile
    lockfile.save_to_path(&lockfile_path).unwrap();
    assert!(lockfile_path.exists());

    // Load lockfile
    let loaded_lockfile = Lockfile::load_from_path(&lockfile_path).unwrap();
    assert_eq!(loaded_lockfile.packages.len(), 1);
    assert_eq!(loaded_lockfile.version, "1.0.0");

    // Get package
    let retrieved = loaded_lockfile.get_package("rig-mcp").unwrap();
    assert_eq!(retrieved.version, "0.1.0");
    assert_eq!(retrieved.checksum, "abc123def456");

    // Update package (add another)
    let mut updated_lockfile = loaded_lockfile;
    let another_package = InstalledPackage {
        name: "api-endpoint".to_string(),
        full_name: "api-endpoint-templates".to_string(),
        version: "1.0.0".to_string(),
        checksum: "xyz789".to_string(),
        source: "registry".to_string(),
        path: ".ggen/packages/api-endpoint".to_string(),
        installed_at: chrono::Utc::now().to_rfc3339(),
        dependencies: vec![],
    };

    updated_lockfile.add_package(another_package);
    updated_lockfile.save_to_path(&lockfile_path).unwrap();

    // Verify update
    let reloaded = Lockfile::load_from_path(&lockfile_path).unwrap();
    assert_eq!(reloaded.packages.len(), 2);

    // Remove package
    let mut final_lockfile = reloaded;
    let removed = final_lockfile.remove_package("rig-mcp");
    assert!(removed.is_some());
    assert_eq!(final_lockfile.packages.len(), 1);

    final_lockfile.save_to_path(&lockfile_path).unwrap();

    // Verify removal
    let final_loaded = Lockfile::load_from_path(&lockfile_path).unwrap();
    assert_eq!(final_loaded.packages.len(), 1);
    assert!(!final_loaded.has_package("rig-mcp"));
    assert!(final_loaded.has_package("api-endpoint"));
}

#[test]
fn test_marketplace_workflow_end_to_end() {
    let temp_dir = TempDir::new().unwrap();
    let registry_path = temp_dir.path().join("packages.toml");
    let lockfile_path = temp_dir.path().join("lock.json");

    // Step 1: Create registry
    let registry = create_test_registry();
    let registry_toml = toml::to_string(&registry).unwrap();
    fs::write(&registry_path, registry_toml).unwrap();

    // Step 2: Load registry
    let loaded_registry = Registry::load_from_path_sync(&registry_path).unwrap();
    assert_eq!(loaded_registry.packages.len(), 2);

    // Step 3: Search for package
    let search_results = loaded_registry.search("rig-mcp", 10);
    assert_eq!(search_results.len(), 1);
    let package = search_results[0];

    // Step 4: Simulate installation - add to lockfile
    let mut lockfile = Lockfile::new();
    let installed_package = InstalledPackage {
        name: package.name.clone(),
        full_name: package.full_name.clone(),
        version: package.version.clone(),
        checksum: "production-checksum".to_string(),
        source: "registry".to_string(),
        path: format!(".ggen/packages/{}", package.name),
        installed_at: chrono::Utc::now().to_rfc3339(),
        dependencies: package.dependencies.clone(),
    };

    lockfile.add_package(installed_package);
    lockfile.save_to_path(&lockfile_path).unwrap();

    // Step 5: Verify installation
    let installed_lockfile = Lockfile::load_from_path(&lockfile_path).unwrap();
    assert!(installed_lockfile.has_package("rig-mcp"));

    let installed_pkg = installed_lockfile.get_package("rig-mcp").unwrap();
    assert_eq!(installed_pkg.version, "0.1.0");
    assert_eq!(installed_pkg.source, "registry");

    // Step 6: List installed packages
    let all_packages = installed_lockfile.list_packages();
    assert_eq!(all_packages.len(), 1);

    // Step 7: Simulate uninstallation
    let mut uninstall_lockfile = installed_lockfile;
    let removed = uninstall_lockfile.remove_package("rig-mcp");
    assert!(removed.is_some());

    uninstall_lockfile.save_to_path(&lockfile_path).unwrap();

    // Step 8: Verify uninstallation
    let final_lockfile = Lockfile::load_from_path(&lockfile_path).unwrap();
    assert!(!final_lockfile.has_package("rig-mcp"));
    assert_eq!(final_lockfile.packages.len(), 0);
}

#[test]
fn test_lockfile_concurrent_access() {
    let temp_dir = TempDir::new().unwrap();
    let lockfile_path = temp_dir.path().join("lock.json");

    // Initial lockfile
    let mut lockfile = Lockfile::new();
    lockfile.save_to_path(&lockfile_path).unwrap();

    // Simulate concurrent package installations
    let package1 = InstalledPackage {
        name: "pkg1".to_string(),
        full_name: "package-1".to_string(),
        version: "1.0.0".to_string(),
        checksum: "check1".to_string(),
        source: "registry".to_string(),
        path: ".ggen/packages/pkg1".to_string(),
        installed_at: chrono::Utc::now().to_rfc3339(),
        dependencies: vec![],
    };

    let package2 = InstalledPackage {
        name: "pkg2".to_string(),
        full_name: "package-2".to_string(),
        version: "2.0.0".to_string(),
        checksum: "check2".to_string(),
        source: "registry".to_string(),
        path: ".ggen/packages/pkg2".to_string(),
        installed_at: chrono::Utc::now().to_rfc3339(),
        dependencies: vec![],
    };

    // Load, modify, save (simulating separate operations)
    let mut lock1 = Lockfile::load_from_path(&lockfile_path).unwrap();
    lock1.add_package(package1);
    lock1.save_to_path(&lockfile_path).unwrap();

    let mut lock2 = Lockfile::load_from_path(&lockfile_path).unwrap();
    lock2.add_package(package2);
    lock2.save_to_path(&lockfile_path).unwrap();

    // Verify final state
    let final_lock = Lockfile::load_from_path(&lockfile_path).unwrap();
    // Note: Last write wins (pkg2), demonstrating need for proper locking in production
    assert!(final_lock.has_package("pkg2"));
    // pkg1 might be lost due to concurrent write - this test documents current behavior
}

#[test]
fn test_registry_package_retrieval() {
    let registry = create_test_registry();

    // Test get_package by name
    let pkg = registry.get_package("rig-mcp");
    assert!(pkg.is_some());
    assert_eq!(pkg.unwrap().version, "0.1.0");

    // Test get_package by full_name
    let pkg = registry.get_package("rig-mcp-integration");
    assert!(pkg.is_some());
    assert_eq!(pkg.unwrap().name, "rig-mcp");

    // Test non-existent package
    let pkg = registry.get_package("non-existent");
    assert!(pkg.is_none());
}

#[test]
fn test_lockfile_persistence_format() {
    let temp_dir = TempDir::new().unwrap();
    let lockfile_path = temp_dir.path().join("lock.json");

    let mut lockfile = Lockfile::new();
    let package = InstalledPackage {
        name: "test-pkg".to_string(),
        full_name: "test-package".to_string(),
        version: "1.0.0".to_string(),
        checksum: "abc123".to_string(),
        source: "registry".to_string(),
        path: ".ggen/packages/test-pkg".to_string(),
        installed_at: "2025-10-12T00:00:00Z".to_string(),
        dependencies: vec!["dep1".to_string(), "dep2".to_string()],
    };

    lockfile.add_package(package);
    lockfile.save_to_path(&lockfile_path).unwrap();

    // Read raw JSON to verify format
    let content = fs::read_to_string(&lockfile_path).unwrap();
    assert!(content.contains("\"version\": \"1.0.0\""));
    assert!(content.contains("\"test-pkg\""));
    assert!(content.contains("\"checksum\": \"abc123\""));
    assert!(content.contains("\"dependencies\""));

    // Verify it's valid JSON
    let parsed: serde_json::Value = serde_json::from_str(&content).unwrap();
    assert!(parsed["packages"]["test-pkg"]["checksum"].is_string());
}

#[test]
fn test_marketplace_error_handling() {
    let temp_dir = TempDir::new().unwrap();

    // Test loading non-existent registry
    let non_existent = temp_dir.path().join("missing.toml");
    let result = Registry::load_from_path_sync(&non_existent);
    assert!(result.is_err());

    // Test loading empty lockfile (should return new lockfile)
    let lockfile_path = temp_dir.path().join("lock.json");
    let result = Lockfile::load_from_path(&lockfile_path);
    assert!(result.is_ok());
    assert_eq!(result.unwrap().packages.len(), 0);

    // Test saving to invalid path (permission denied simulation)
    // Note: This is platform-dependent and may need adjustment
    let mut lockfile = Lockfile::new();
    let invalid_path = temp_dir.path().join("subdir/nested/lock.json");
    // Should succeed because parent dirs are created
    let result = lockfile.save_to_path(&invalid_path);
    assert!(result.is_ok());
}

#[cfg(test)]
mod production_readiness_tests {
    use super::*;

    #[test]
    fn test_production_registry_structure() {
        let registry = create_test_registry();

        // Verify all required fields are present
        for package in &registry.packages {
            assert!(!package.name.is_empty());
            assert!(!package.version.is_empty());
            assert!(!package.description.is_empty());
            assert!(!package.category.is_empty());
            assert!(!package.author.is_empty());
            assert!(!package.repository.is_empty());
            assert!(!package.path.is_empty());
            assert!(!package.license.is_empty());
        }
    }

    #[test]
    fn test_production_lockfile_validation() {
        let mut lockfile = Lockfile::new();

        let package = InstalledPackage {
            name: "prod-pkg".to_string(),
            full_name: "production-package".to_string(),
            version: "1.0.0".to_string(),
            checksum: "sha256:abc123def456".to_string(),
            source: "registry".to_string(),
            path: ".ggen/packages/prod-pkg".to_string(),
            installed_at: chrono::Utc::now().to_rfc3339(),
            dependencies: vec![],
        };

        lockfile.add_package(package);

        // Verify production-ready attributes
        assert!(!lockfile.version.is_empty());
        assert!(!lockfile.updated_at.is_empty());

        let installed = lockfile.get_package("prod-pkg").unwrap();
        assert!(installed.checksum.starts_with("sha256:"));
        assert!(!installed.installed_at.is_empty());
    }

    #[test]
    fn test_marketplace_scalability() {
        let temp_dir = TempDir::new().unwrap();
        let lockfile_path = temp_dir.path().join("lock.json");

        let mut lockfile = Lockfile::new();

        // Test with 100 packages
        for i in 0..100 {
            let package = InstalledPackage {
                name: format!("pkg-{}", i),
                full_name: format!("package-{}", i),
                version: "1.0.0".to_string(),
                checksum: format!("checksum-{}", i),
                source: "registry".to_string(),
                path: format!(".ggen/packages/pkg-{}", i),
                installed_at: chrono::Utc::now().to_rfc3339(),
                dependencies: vec![],
            };
            lockfile.add_package(package);
        }

        // Save and load large lockfile
        lockfile.save_to_path(&lockfile_path).unwrap();
        let loaded = Lockfile::load_from_path(&lockfile_path).unwrap();

        assert_eq!(loaded.packages.len(), 100);

        // Verify retrieval performance
        for i in 0..100 {
            let pkg_name = format!("pkg-{}", i);
            assert!(loaded.has_package(&pkg_name));
        }
    }
}
