use clap::Parser;
use newton_cli::commands::policy::PolicyCommand;

#[test]
fn test_validate_non_empty_path_valid() {
    // Test valid path through clap parsing
    let args = vec![
        "policy",
        "deploy",
        "--private-key",
        "0x123",
        "--rpc-url",
        "https://example.com",
        "--policy-data-address",
        "0x0000000000000000000000000000000000000001",
        "--policy-cids",
        "/valid/path",
    ];

    let result = PolicyCommand::try_parse_from(args);
    if let Err(e) = &result {
        eprintln!("Parse error: {}", e);
    }
    assert!(result.is_ok(), "Valid path should parse successfully");
}

#[test]
fn test_validate_non_empty_path_empty() {
    // Test empty path fails validation
    let args = vec![
        "policy",
        "deploy",
        "--private-key",
        "0x123",
        "--rpc-url",
        "https://example.com",
        "--policy-data-address",
        "0x0000000000000000000000000000000000000001",
        "--policy-cids",
        "",
    ];

    let result = PolicyCommand::try_parse_from(args);
    assert!(result.is_err(), "Empty path should fail validation");
}

#[test]
fn test_validate_non_empty_path_whitespace() {
    // Test whitespace-only path fails validation
    let args = vec![
        "policy",
        "deploy",
        "--private-key",
        "0x123",
        "--rpc-url",
        "https://example.com",
        "--policy-data-address",
        "0x0000000000000000000000000000000000000001",
        "--policy-cids",
        "   ",
    ];

    let result = PolicyCommand::try_parse_from(args);
    assert!(result.is_err(), "Whitespace-only path should fail validation");
}

#[test]
fn test_validate_non_empty_path_valid_with_spaces() {
    // Test path with spaces (but not all whitespace) is valid
    let args = vec![
        "policy",
        "deploy",
        "--private-key",
        "0x123",
        "--rpc-url",
        "https://example.com",
        "--policy-data-address",
        "0x0000000000000000000000000000000000000001",
        "--policy-cids",
        "/path/with spaces",
    ];

    let result = PolicyCommand::try_parse_from(args);
    assert!(result.is_ok(), "Path with spaces should be valid");
}

#[test]
fn test_deploy_command_parsing() {
    // Test that command can be parsed with required arguments
    let args = vec![
        "policy",
        "deploy",
        "--private-key",
        "0x123",
        "--rpc-url",
        "https://example.com",
        "--policy-data-address",
        "0x0000000000000000000000000000000000000001",
        "--policy-cids",
        "test.json",
    ];

    let result = PolicyCommand::try_parse_from(args);
    assert!(result.is_ok(), "Command should parse with all required arguments");
}

#[test]
fn test_deploy_command_parsing_missing_required() {
    // Test that missing required arguments fail
    let args = vec![
        "policy",
        "deploy",
        "--private-key",
        "0x123",
        // Missing rpc-url and policy-data-address
    ];

    let result = PolicyCommand::try_parse_from(args);
    assert!(result.is_err(), "Command should fail with missing required arguments");
}

#[test]
fn test_deploy_command_env_var_support() {
    use std::env;

    // Test that environment variables are read
    env::set_var("PRIVATE_KEY", "0xenv123");
    env::set_var("RPC_URL", "https://env.example.com");

    let args = vec![
        "policy",
        "deploy",
        "--policy-data-address",
        "0x0000000000000000000000000000000000000001",
        "--policy-cids",
        "test.json",
    ];

    let result = PolicyCommand::try_parse_from(args);
    assert!(result.is_ok(), "Command should parse with env vars");

    // Clean up
    env::remove_var("PRIVATE_KEY");
    env::remove_var("RPC_URL");
}
