#[cfg(test)]
mod implementation_tests {
    use crate::default::{ButtonVariant, ButtonSize, ButtonChildProps, BUTTON_CLASS};
    use leptos::prelude::*;
    use leptos_style::Style;

    // ===== COMPREHENSIVE IMPLEMENTATION TESTS =====
    // These tests focus on actual implementation logic and edge cases

    #[test]
    fn test_button_variant_enum_conversions() {
        // Test ButtonVariant From<String> implementation
        assert_eq!(ButtonVariant::from("destructive".to_string()), ButtonVariant::Destructive);
        assert_eq!(ButtonVariant::from("outline".to_string()), ButtonVariant::Outline);
        assert_eq!(ButtonVariant::from("secondary".to_string()), ButtonVariant::Secondary);
        assert_eq!(ButtonVariant::from("ghost".to_string()), ButtonVariant::Ghost);
        assert_eq!(ButtonVariant::from("link".to_string()), ButtonVariant::Link);
        assert_eq!(ButtonVariant::from("invalid".to_string()), ButtonVariant::Default);
        assert_eq!(ButtonVariant::from("".to_string()), ButtonVariant::Default);
    }

    #[test]
    fn test_button_size_enum_conversions() {
        // Test ButtonSize From<String> implementation
        assert_eq!(ButtonSize::from("sm".to_string()), ButtonSize::Sm);
        assert_eq!(ButtonSize::from("lg".to_string()), ButtonSize::Lg);
        assert_eq!(ButtonSize::from("icon".to_string()), ButtonSize::Icon);
        assert_eq!(ButtonSize::from("invalid".to_string()), ButtonSize::Default);
        assert_eq!(ButtonSize::from("".to_string()), ButtonSize::Default);
    }

    #[test]
    fn test_button_variant_default() {
        // Test Default implementation for ButtonVariant
        assert_eq!(ButtonVariant::default(), ButtonVariant::Default);
    }

    #[test]
    fn test_button_size_default() {
        // Test Default implementation for ButtonSize
        assert_eq!(ButtonSize::default(), ButtonSize::Default);
    }

    #[test]
    fn test_button_variant_equality() {
        // Test PartialEq implementation for ButtonVariant
        assert_eq!(ButtonVariant::Default, ButtonVariant::Default);
        assert_ne!(ButtonVariant::Default, ButtonVariant::Destructive);
        assert_eq!(ButtonVariant::Outline, ButtonVariant::Outline);
        assert_ne!(ButtonVariant::Ghost, ButtonVariant::Link);
    }

    #[test]
    fn test_button_size_equality() {
        // Test PartialEq implementation for ButtonSize
        assert_eq!(ButtonSize::Default, ButtonSize::Default);
        assert_ne!(ButtonSize::Default, ButtonSize::Sm);
        assert_eq!(ButtonSize::Icon, ButtonSize::Icon);
        assert_ne!(ButtonSize::Lg, ButtonSize::Sm);
    }

    #[test]
    fn test_button_variant_clone() {
        // Test Clone implementation for ButtonVariant
        let variant = ButtonVariant::Destructive;
        let cloned = variant.clone();
        assert_eq!(variant, cloned);
    }

    #[test]
    fn test_button_size_clone() {
        // Test Clone implementation for ButtonSize
        let size = ButtonSize::Lg;
        let cloned = size.clone();
        assert_eq!(size, cloned);
    }

    #[test]
    fn test_button_variant_debug() {
        // Test Debug implementation for ButtonVariant
        let variant = ButtonVariant::Outline;
        let debug_str = format!("{:?}", variant);
        assert!(debug_str.contains("Outline"));
    }

    #[test]
    fn test_button_size_debug() {
        // Test Debug implementation for ButtonSize
        let size = ButtonSize::Icon;
        let debug_str = format!("{:?}", size);
        assert!(debug_str.contains("Icon"));
    }

    #[test]
    fn test_button_child_props_creation() {
        // Test ButtonChildProps creation and field access
        let props = ButtonChildProps {
            class: "test-class".to_string(),
            id: "test-id".to_string(),
            style: "color: red".to_string(),
            disabled: true,
            r#type: "submit".to_string(),
            onclick: Some(Callback::new(|_| {})),
        };

        assert_eq!(props.class, "test-class");
        assert_eq!(props.id, "test-id");
        assert_eq!(props.style, "color: red");
        assert!(props.disabled);
        assert_eq!(props.r#type, "submit");
        assert!(props.onclick.is_some());
    }

    #[test]
    fn test_button_child_props_clone() {
        // Test ButtonChildProps Clone implementation
        let props = ButtonChildProps {
            class: "test-class".to_string(),
            id: "test-id".to_string(),
            style: "color: red".to_string(),
            disabled: false,
            r#type: "button".to_string(),
            onclick: None,
        };

        let cloned = props.clone();
        assert_eq!(props.class, cloned.class);
        assert_eq!(props.id, cloned.id);
        assert_eq!(props.style, cloned.style);
        assert_eq!(props.disabled, cloned.disabled);
        assert_eq!(props.r#type, cloned.r#type);
        assert_eq!(props.onclick.is_some(), cloned.onclick.is_some());
    }

    #[test]
    fn test_button_child_props_debug() {
        // Test ButtonChildProps Debug implementation
        let props = ButtonChildProps {
            class: "test-class".to_string(),
            id: "test-id".to_string(),
            style: "color: red".to_string(),
            disabled: true,
            r#type: "button".to_string(),
            onclick: None,
        };

        let debug_str = format!("{:?}", props);
        assert!(debug_str.contains("test-class"));
        assert!(debug_str.contains("test-id"));
    }

    #[test]
    fn test_button_class_constant() {
        // Test BUTTON_CLASS constant
        assert!(BUTTON_CLASS.contains("inline-flex"));
        assert!(BUTTON_CLASS.contains("items-center"));
        assert!(BUTTON_CLASS.contains("justify-center"));
        assert!(BUTTON_CLASS.contains("rounded-md"));
        assert!(BUTTON_CLASS.contains("transition-colors"));
    }

    #[test]
    fn test_button_click_handler_with_callback() {
        // Test click handler with callback
        let click_count = RwSignal::new(0);
        let callback = Callback::new(move |_| {
            click_count.update(|count| *count += 1);
        });

        // Simulate the click handler logic
        callback.run(());
        assert_eq!(click_count.get(), 1);

        callback.run(());
        assert_eq!(click_count.get(), 2);
    }

    #[test]
    fn test_button_click_handler_without_callback() {
        // Test click handler without callback (should not panic)
        let callback: Option<Callback<()>> = None;
        
        // This should not panic
        if let Some(cb) = &callback {
            cb.run(());
        }
        
        // Test passes if no panic occurs
        assert!(true);
    }

    #[test]
    fn test_button_computed_class_default_variant() {
        // Test computed class generation for default variant
        let variant = Some(ButtonVariant::Default);
        let size = Some(ButtonSize::Default);
        let class = Some("custom-class".to_string());

        // Simulate the computed class logic
        let variant_class = match variant.unwrap_or_default() {
            ButtonVariant::Default => "bg-primary text-primary-foreground hover:bg-primary/90",
            _ => "",
        };
        
        let size_class = match size.unwrap_or_default() {
            ButtonSize::Default => "h-10 px-4 py-2",
            _ => "",
        };

        let computed_class = format!("{} {} {} {}", 
            BUTTON_CLASS, 
            variant_class, 
            size_class, 
            class.unwrap_or_default()
        );

        assert!(computed_class.contains("bg-primary"));
        assert!(computed_class.contains("h-10 px-4 py-2"));
        assert!(computed_class.contains("custom-class"));
    }

    #[test]
    fn test_button_computed_class_all_variants() {
        // Test computed class generation for all variants
        let variants = vec![
            (ButtonVariant::Default, "bg-primary text-primary-foreground hover:bg-primary/90"),
            (ButtonVariant::Destructive, "bg-destructive text-destructive-foreground hover:bg-destructive/90"),
            (ButtonVariant::Outline, "border border-input bg-background hover:bg-accent hover:text-accent-foreground"),
            (ButtonVariant::Secondary, "bg-secondary text-secondary-foreground hover:bg-secondary/80"),
            (ButtonVariant::Ghost, "hover:bg-accent hover:text-accent-foreground"),
            (ButtonVariant::Link, "text-primary underline-offset-4 hover:underline"),
        ];

        for (variant, expected_class) in variants {
            let variant_class = match variant {
                ButtonVariant::Default => "bg-primary text-primary-foreground hover:bg-primary/90",
                ButtonVariant::Destructive => "bg-destructive text-destructive-foreground hover:bg-destructive/90",
                ButtonVariant::Outline => "border border-input bg-background hover:bg-accent hover:text-accent-foreground",
                ButtonVariant::Secondary => "bg-secondary text-secondary-foreground hover:bg-secondary/80",
                ButtonVariant::Ghost => "hover:bg-accent hover:text-accent-foreground",
                ButtonVariant::Link => "text-primary underline-offset-4 hover:underline",
            };
            
            assert_eq!(variant_class, expected_class, "Variant {:?} should have correct class", variant);
        }
    }

    #[test]
    fn test_button_computed_class_all_sizes() {
        // Test computed class generation for all sizes
        let sizes = vec![
            (ButtonSize::Default, "h-10 px-4 py-2"),
            (ButtonSize::Sm, "h-9 rounded-md px-3"),
            (ButtonSize::Lg, "h-11 rounded-md px-8"),
            (ButtonSize::Icon, "h-10 w-10"),
        ];

        for (size, expected_class) in sizes {
            let size_class = match size {
                ButtonSize::Default => "h-10 px-4 py-2",
                ButtonSize::Sm => "h-9 rounded-md px-3",
                ButtonSize::Lg => "h-11 rounded-md px-8",
                ButtonSize::Icon => "h-10 w-10",
            };
            
            assert_eq!(size_class, expected_class, "Size {:?} should have correct class", size);
        }
    }

    #[test]
    fn test_button_computed_class_with_none_props() {
        // Test computed class generation with None props
        let variant: Option<ButtonVariant> = None;
        let size: Option<ButtonSize> = None;
        let class: Option<String> = None;

        // Simulate the computed class logic with None props
        let variant_class = match variant.unwrap_or_default() {
            ButtonVariant::Default => "bg-primary text-primary-foreground hover:bg-primary/90",
            _ => "",
        };
        
        let size_class = match size.unwrap_or_default() {
            ButtonSize::Default => "h-10 px-4 py-2",
            _ => "",
        };

        let computed_class = format!("{} {} {} {}", 
            BUTTON_CLASS, 
            variant_class, 
            size_class, 
            class.unwrap_or_default()
        );

        assert!(computed_class.contains("bg-primary"));
        assert!(computed_class.contains("h-10 px-4 py-2"));
        assert!(!computed_class.contains("custom-class"));
    }

    #[test]
    fn test_button_as_child_callback_execution() {
        // Test as_child callback execution
        let callback_executed = RwSignal::new(false);
        let as_child_callback = Callback::new(move |props: ButtonChildProps| {
            callback_executed.set(true);
            assert_eq!(props.class, "test-class");
            assert_eq!(props.id, "test-id");
            assert!(props.disabled);
            assert_eq!(props.r#type, "button");
            view! { <div>"Child Component"</div> }.into_any()
        });

        let child_props = ButtonChildProps {
            class: "test-class".to_string(),
            id: "test-id".to_string(),
            style: "color: red".to_string(),
            disabled: true,
            r#type: "button".to_string(),
            onclick: Some(Callback::new(|_| {})),
        };

        // Execute the callback
        let _result = as_child_callback.run(child_props);
        assert!(callback_executed.get(), "as_child callback should have been executed");
    }

    #[test]
    fn test_button_style_signal_handling() {
        // Test style signal handling
        let style_signal = RwSignal::new(Style::new());
        let style = Style::new();
        style_signal.set(style);

        let style_string = style_signal.get().to_string();
        // Style should be empty initially
        assert_eq!(style_string, "");
    }

    #[test]
    fn test_button_disabled_signal_handling() {
        // Test disabled signal handling
        let disabled_signal = RwSignal::new(false);
        assert!(!disabled_signal.get());

        disabled_signal.set(true);
        assert!(disabled_signal.get());

        disabled_signal.set(false);
        assert!(!disabled_signal.get());
    }

    #[test]
    fn test_button_id_prop_handling() {
        // Test id prop handling
        let id_prop = Some("test-button-id".to_string());
        assert_eq!(id_prop.unwrap_or_default(), "test-button-id");

        let id_prop_none: Option<String> = None;
        assert_eq!(id_prop_none.unwrap_or_default(), "");
    }

    #[test]
    fn test_button_class_prop_handling() {
        // Test class prop handling
        let class_prop = Some("custom-button-class".to_string());
        assert_eq!(class_prop.unwrap_or_default(), "custom-button-class");

        let class_prop_none: Option<String> = None;
        assert_eq!(class_prop_none.unwrap_or_default(), "");
    }

    #[test]
    fn test_button_children_handling() {
        // Test children handling - simplified test
        let children: Option<Children> = None;
        assert!(children.is_none());
        
        // Test that we can create a simple children option
        let has_children = true;
        assert!(has_children);
    }

    #[test]
    fn test_button_callback_clone() {
        // Test callback cloning
        let original_callback = Callback::new(|_: ()| {});
        let cloned_callback = original_callback.clone();
        
        // Both should be valid callbacks
        assert!(true); // Test passes if no panic occurs during clone
    }

    #[test]
    fn test_button_signal_derive() {
        // Test Signal::derive functionality
        let variant = RwSignal::new(ButtonVariant::Default);
        let size = RwSignal::new(ButtonSize::Default);
        let class = RwSignal::new("test-class".to_string());

        let computed_class = Signal::derive(move || {
            let variant_class = match variant.get() {
                ButtonVariant::Default => "bg-primary text-primary-foreground hover:bg-primary/90",
                ButtonVariant::Destructive => "bg-destructive text-destructive-foreground hover:bg-destructive/90",
                ButtonVariant::Outline => "border border-input bg-background hover:bg-accent hover:text-accent-foreground",
                ButtonVariant::Secondary => "bg-secondary text-secondary-foreground hover:bg-secondary/80",
                ButtonVariant::Ghost => "hover:bg-accent hover:text-accent-foreground",
                ButtonVariant::Link => "text-primary underline-offset-4 hover:underline",
            };
            
            let size_class = match size.get() {
                ButtonSize::Default => "h-10 px-4 py-2",
                ButtonSize::Sm => "h-9 rounded-md px-3",
                ButtonSize::Lg => "h-11 rounded-md px-8",
                ButtonSize::Icon => "h-10 w-10",
            };

            format!("{} {} {} {}", 
                BUTTON_CLASS, 
                variant_class, 
                size_class, 
                class.get()
            )
        });

        let result = computed_class.get();
        assert!(result.contains("bg-primary"));
        assert!(result.contains("h-10 px-4 py-2"));
        assert!(result.contains("test-class"));

        // Test reactivity
        variant.set(ButtonVariant::Destructive);
        let new_result = computed_class.get();
        // Debug: print the actual result to understand what's happening
        println!("New result after variant change: '{}'", new_result);
        // The computed class should update to reflect the new variant
        assert!(new_result.contains("bg-destructive"), "Expected 'bg-destructive' in '{}'", new_result);
        assert!(!new_result.contains("bg-primary"), "Expected no 'bg-primary' in '{}'", new_result);
    }

    #[test]
    fn test_button_edge_cases() {
        // Test edge cases and error conditions
        
        // Test with empty strings
        let empty_variant = ButtonVariant::from("".to_string());
        assert_eq!(empty_variant, ButtonVariant::Default);

        let empty_size = ButtonSize::from("".to_string());
        assert_eq!(empty_size, ButtonSize::Default);

        // Test with invalid strings
        let invalid_variant = ButtonVariant::from("invalid_variant".to_string());
        assert_eq!(invalid_variant, ButtonVariant::Default);

        let invalid_size = ButtonSize::from("invalid_size".to_string());
        assert_eq!(invalid_size, ButtonSize::Default);

        // Test with very long strings
        let long_string = "a".repeat(1000);
        let long_variant = ButtonVariant::from(long_string.clone());
        assert_eq!(long_variant, ButtonVariant::Default);

        let long_size = ButtonSize::from(long_string);
        assert_eq!(long_size, ButtonSize::Default);
    }

    #[test]
    fn test_button_memory_management() {
        // Test memory management and cleanup
        let signal = RwSignal::new(0);
        let callback = Callback::new(move |_| {
            signal.update(|count| *count += 1);
        });

        // Execute callback multiple times
        for _ in 0..100 {
            callback.run(());
        }

        assert_eq!(signal.get(), 100);
        
        // Test that signals can be dropped without issues
        let _ = signal;
        let _ = callback;
        
        // Test passes if no memory leaks or panics occur
        assert!(true);
    }

    #[test]
    fn test_button_performance_characteristics() {
        // Test performance characteristics
        let start = std::time::Instant::now();
        
        // Create many button variants
        let variants = vec![
            ButtonVariant::Default,
            ButtonVariant::Destructive,
            ButtonVariant::Outline,
            ButtonVariant::Secondary,
            ButtonVariant::Ghost,
            ButtonVariant::Link,
        ];

        let sizes = vec![
            ButtonSize::Default,
            ButtonSize::Sm,
            ButtonSize::Lg,
            ButtonSize::Icon,
        ];

        // Test enum operations performance
        for _ in 0..1000 {
            for variant in &variants {
                let _ = format!("{:?}", variant);
                let _ = variant.clone();
            }
            
            for size in &sizes {
                let _ = format!("{:?}", size);
                let _ = size.clone();
            }
        }

        let duration = start.elapsed();
        
        // Should complete quickly (less than 100ms for 1000 iterations)
        assert!(duration.as_millis() < 100, "Enum operations should be fast");
    }
}
