#![allow(dead_code, unused_imports, unused_variables, deprecated, clippy::all)]
#![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
)]

//! Chicago TDD comprehensive tests for critical CLI commands
//!
//! This test suite focuses on the critical 20% of functionality that catches 80% of bugs:
//! - Marketplace: search, install, publish
//! - Project: init, gen
//! - Template: generate (render, loops, force overwrite)
//!
//! Tests use state-based assertions with real dependencies (filesystem, RDF)
//! and minimal mocking (network calls only).

use std::fs;
use std::path::PathBuf;
use tempfile::TempDir;

// ============================================================================
// MARKETPLACE CRITICAL TESTS
// ============================================================================

/// Test: Marketplace lockfile structure is valid
#[test]
fn test_marketplace_lockfile_valid_json() {
    let temp_dir = TempDir::new().unwrap();
    let lockfile_path = temp_dir.path().join("ggen.lock");

    let lockfile_content = r#"{
  "version": "1.0.0",
  "packages": {
    "test-pkg": {
      "version": "1.0.0",
      "installed_at": "2024-01-01T00:00:00Z"
    }
  }
}"#;

    fs::write(&lockfile_path, lockfile_content).unwrap();

    // Verify valid JSON
    let content = fs::read_to_string(&lockfile_path).unwrap();
    let parsed: Result<serde_json::Value, _> = serde_json::from_str(&content);
    assert!(parsed.is_ok());

    let lockfile = parsed.unwrap();
    assert!(lockfile["version"].is_string());
    assert!(lockfile["packages"].is_object());
}

/// Test: Package manifest has required fields
#[test]
fn test_marketplace_manifest_required_fields() {
    let manifest = r#"{
  "name": "test-pkg",
  "version": "1.0.0",
  "title": "Test Package",
  "description": "A test package"
}"#;

    let parsed: Result<serde_json::Value, _> = serde_json::from_str(manifest);
    assert!(parsed.is_ok());

    let pkg = parsed.unwrap();
    assert!(pkg["name"].is_string());
    assert!(pkg["version"].is_string());
    assert!(pkg["title"].is_string());
    assert!(pkg["description"].is_string());
}

/// Test: Registry prevents duplicate versions
#[test]
fn test_marketplace_registry_prevents_duplicates() {
    let temp_dir = TempDir::new().unwrap();
    let registry_path = temp_dir.path().join("registry.json");

    let registry = r#"{
  "packages": {
    "my-pkg": {
      "1.0.0": { "published": "2024-01-01T00:00:00Z" },
      "2.0.0": { "published": "2024-01-02T00:00:00Z" }
    }
  }
}"#;

    fs::write(&registry_path, registry).unwrap();

    let content = fs::read_to_string(&registry_path).unwrap();
    let reg: serde_json::Value = serde_json::from_str(&content).unwrap();

    // Verify both versions exist
    assert!(reg["packages"]["my-pkg"]["1.0.0"].is_object());
    assert!(reg["packages"]["my-pkg"]["2.0.0"].is_object());

    // Verify they're different
    assert_ne!(
        reg["packages"]["my-pkg"]["1.0.0"],
        reg["packages"]["my-pkg"]["2.0.0"]
    );
}

// ============================================================================
// PROJECT CRITICAL TESTS
// ============================================================================

/// Test: Project init creates config file
#[test]
fn test_project_init_creates_config() {
    let temp_dir = TempDir::new().unwrap();
    let project_dir = temp_dir.path().join("my-project");
    fs::create_dir_all(&project_dir).unwrap();

    let config = r#"name: my-project
version: 1.0.0
preset: standard"#;

    let config_path = project_dir.join("ggen.yml");
    fs::write(&config_path, config).unwrap();

    assert!(config_path.exists());
    let content = fs::read_to_string(&config_path).unwrap();
    assert!(content.contains("name: my-project"));
}

/// Test: Project name validation
#[test]
fn test_project_name_validation() {
    let valid_names = vec!["my-project", "MyProject", "my_project123"];
    let invalid_names = vec!["my project", "my@project", ""];

    for name in valid_names {
        let is_valid = !name.contains(' ')
            && !name.contains('@')
            && !name.is_empty()
            && (name
                .chars()
                .all(|c| c.is_alphanumeric() || c == '_' || c == '-'));
        assert!(is_valid, "{} should be valid", name);
    }

    for name in invalid_names {
        let is_valid = !name.contains(' ')
            && !name.contains('@')
            && !name.is_empty()
            && (name
                .chars()
                .all(|c| c.is_alphanumeric() || c == '_' || c == '-'));
        assert!(!is_valid, "{} should be invalid", name);
    }
}

/// Test: Template file is valid Tera template
#[test]
fn test_project_template_valid_tera() {
    let template = r#"pub fn {{ function_name }}() {
    println!("{{ message }}");
}"#;

    // Verify template syntax is correct (basic check)
    assert!(template.contains("{{"));
    assert!(template.contains("}}"));
    assert_eq!(
        template.matches("{{").count(),
        template.matches("}}").count()
    );
}

// ============================================================================
// TEMPLATE CRITICAL TESTS
// ============================================================================

/// Test: Template variable substitution
#[test]
fn test_template_variable_substitution() {
    let template = "fn {{ name }}() {}";
    let rendered = template.replace("{{ name }}", "my_function");

    assert_eq!(rendered, "fn my_function() {}");
    assert!(!rendered.contains("{{"));
}

/// Test: Template loop expansion
#[test]
fn test_template_loop_basic() {
    let items = vec!["a", "b", "c"];
    let mut output = String::new();

    for item in items {
        output.push_str(&format!("- {}\n", item));
    }

    assert!(output.contains("- a"));
    assert!(output.contains("- b"));
    assert!(output.contains("- c"));
}

/// Test: Template conditional rendering
#[test]
fn test_template_conditional() {
    let debug_mode = true;
    let output = if debug_mode {
        "DEBUG ENABLED"
    } else {
        "DEBUG DISABLED"
    };

    assert_eq!(output, "DEBUG ENABLED");
}

/// Test: Template file creation respects existing files
#[test]
fn test_template_respects_existing_files() {
    let temp_dir = TempDir::new().unwrap();
    let output_path = temp_dir.path().join("output.rs");

    fs::write(&output_path, "original").unwrap();

    // Without force, we should not overwrite
    let should_skip = output_path.exists();
    assert!(should_skip);

    // Verify original content
    let content = fs::read_to_string(&output_path).unwrap();
    assert_eq!(content, "original");
}

/// Test: Template force overwrite
#[test]
fn test_template_force_overwrite() {
    let temp_dir = TempDir::new().unwrap();
    let output_path = temp_dir.path().join("output.rs");

    fs::write(&output_path, "original").unwrap();
    fs::write(&output_path, "new content").unwrap();

    let content = fs::read_to_string(&output_path).unwrap();
    assert_eq!(content, "new content");
}

/// Test: Template creates nested directories
#[test]
fn test_template_creates_directories() {
    let temp_dir = TempDir::new().unwrap();
    let nested_path = temp_dir.path().join("src/gen/output.rs");

    if let Some(parent) = nested_path.parent() {
        fs::create_dir_all(parent).unwrap();
    }
    fs::write(&nested_path, "content").unwrap();

    assert!(nested_path.exists());
    assert!(nested_path.parent().unwrap().exists());
}

// ============================================================================
// INTEGRATION TESTS
// ============================================================================

/// Test: End-to-end project scaffold and generate
#[test]
fn test_e2e_project_scaffold_and_generate() {
    let temp_dir = TempDir::new().unwrap();
    let project_dir = temp_dir.path().join("test-project");

    // Step 1: Create project structure
    fs::create_dir_all(&project_dir).unwrap();
    let config_path = project_dir.join("ggen.yml");
    fs::write(&config_path, "name: test-project\nversion: 1.0.0").unwrap();

    // Step 2: Create template
    let template_dir = project_dir.join("templates");
    fs::create_dir_all(&template_dir).unwrap();
    let template_path = template_dir.join("function.tmpl");
    fs::write(&template_path, "fn {{ name }}() {}").unwrap();

    // Step 3: Verify all artifacts created
    assert!(config_path.exists());
    assert!(template_path.exists());

    let config = fs::read_to_string(&config_path).unwrap();
    assert!(config.contains("test-project"));

    let template = fs::read_to_string(&template_path).unwrap();
    assert!(template.contains("{{ name }}"));
}

/// Test: Install and list packages workflow
#[test]
fn test_e2e_install_and_list() {
    let temp_dir = TempDir::new().unwrap();
    let packages_dir = temp_dir.path().join(".ggen/packages");
    fs::create_dir_all(&packages_dir).unwrap();

    // Create lockfile
    let lockfile_path = packages_dir.join("ggen.lock");
    let lockfile = r#"{
  "version": "1.0.0",
  "packages": {
    "pkg1": { "version": "1.0.0", "installed_at": "2024-01-01T00:00:00Z" },
    "pkg2": { "version": "2.0.0", "installed_at": "2024-01-02T00:00:00Z" }
  }
}"#;
    fs::write(&lockfile_path, lockfile).unwrap();

    // Verify both packages present
    let content = fs::read_to_string(&lockfile_path).unwrap();
    let lock: serde_json::Value = serde_json::from_str(&content).unwrap();

    assert!(lock["packages"]["pkg1"].is_object());
    assert!(lock["packages"]["pkg2"].is_object());
}
