#import <Foundation/Foundation.h>
#import "Knotapi.h"

/**
 * SimpleEventTest - Command line test for event handling logic
 * 
 * This test can be run without Xcode and validates the core event handling
 * functionality and safety mechanisms.
 */
int main(int argc, const char * argv[]) {
    @autoreleasepool {
        NSLog(@"🧪 Starting Simple Event Test...");
        
        // Test 1: Bridge initialization
        NSLog(@"📋 Test 1: Bridge initialization");
        Knotapi *knotapi = [[Knotapi alloc] init];
        if (knotapi.isBridgeActive && !knotapi.isClosing) {
            NSLog(@"✅ Bridge initializes correctly (active: %@, closing: %@)", 
                  knotapi.isBridgeActive ? @"YES" : @"NO",
                  knotapi.isClosing ? @"YES" : @"NO");
        } else {
            NSLog(@"❌ Bridge initialization failed");
            return 1;
        }
        
        // Test 2: Bridge state management
        NSLog(@"📋 Test 2: Bridge state management");
        knotapi.isBridgeActive = NO;
        knotapi.isClosing = YES;
        if (![knotapi isBridgeReadyForEvents]) {
            NSLog(@"✅ Bridge correctly reports not ready when inactive and closing");
        } else {
            NSLog(@"❌ Bridge incorrectly reports ready when inactive and closing");
            return 1;
        }
        
        // Test 3: Dictionary safety
        NSLog(@"📋 Test 3: Dictionary safety");
        NSMutableDictionary *testDict = [NSMutableDictionary dictionary];
        testDict[@"validKey"] = @"validValue";
        testDict[@"nullKey"] = [NSNull null];
        testDict[@"nilKey"] = nil;
        
        NSDictionary *safeDict = [knotapi createSafeDictionaryFromDictionary:testDict];
        if (safeDict.count == 1 && [safeDict[@"validKey"] isEqualToString:@"validValue"]) {
            NSLog(@"✅ Dictionary safety correctly filters out nil/NSNull values");
        } else {
            NSLog(@"❌ Dictionary safety failed (count: %lu, expected: 1)", (unsigned long)safeDict.count);
            return 1;
        }
        
        // Test 4: String safety
        NSLog(@"📋 Test 4: String safety");
        NSMutableDictionary *stringDict = [NSMutableDictionary dictionary];
        [knotapi addSafeStringValue:@"validString" forKey:@"validKey" toDictionary:stringDict];
        [knotapi addSafeStringValue:nil forKey:@"nilKey" toDictionary:stringDict];
        [knotapi addSafeStringValue:(NSString *)[NSNull null] forKey:@"nullKey" toDictionary:stringDict];
        
        if (stringDict.count == 1 && [stringDict[@"validKey"] isEqualToString:@"validString"]) {
            NSLog(@"✅ String safety correctly handles nil/NSNull values");
        } else {
            NSLog(@"❌ String safety failed (count: %lu, expected: 1)", (unsigned long)stringDict.count);
            return 1;
        }
        
        // Test 5: Metadata safety
        NSLog(@"📋 Test 5: Metadata safety");
        NSMutableDictionary *metaDict = [NSMutableDictionary dictionary];
        NSMutableDictionary *testMetaData = [NSMutableDictionary dictionary];
        testMetaData[@"validKey"] = @"validValue";
        testMetaData[@"nullKey"] = [NSNull null];
        
        [knotapi addSafeMetaData:testMetaData forKey:@"metaData" toDictionary:metaDict];
        NSDictionary *resultMetaData = metaDict[@"metaData"];
        if (resultMetaData && resultMetaData.count == 1 && [resultMetaData[@"validKey"] isEqualToString:@"validValue"]) {
            NSLog(@"✅ Metadata safety correctly filters out NSNull values");
        } else {
            NSLog(@"❌ Metadata safety failed");
            return 1;
        }
        
        // Test 6: Nil input handling
        NSLog(@"📋 Test 6: Nil input handling");
        NSDictionary *nilDict = [knotapi createSafeDictionaryFromDictionary:nil];
        if (nilDict && nilDict.count == 0) {
            NSLog(@"✅ Nil input handling works correctly");
        } else {
            NSLog(@"❌ Nil input handling failed");
            return 1;
        }
        
        // Test 7: Non-dictionary input handling
        NSLog(@"📋 Test 7: Non-dictionary input handling");
        NSDictionary *nonDict = [knotapi createSafeDictionaryFromDictionary:(NSDictionary *)@"not a dictionary"];
        if (nonDict && nonDict.count == 0) {
            NSLog(@"✅ Non-dictionary input handling works correctly");
        } else {
            NSLog(@"❌ Non-dictionary input handling failed");
            return 1;
        }
        
        // Test 8: Empty metadata handling
        NSLog(@"📋 Test 8: Empty metadata handling");
        NSMutableDictionary *emptyMetaDict = [NSMutableDictionary dictionary];
        [knotapi addSafeMetaData:@{} forKey:@"emptyMeta" toDictionary:emptyMetaDict];
        if (emptyMetaDict.count == 0) {
            NSLog(@"✅ Empty metadata handling works correctly (not added)");
        } else {
            NSLog(@"❌ Empty metadata handling failed");
            return 1;
        }
        
        // Test 9: Bridge state transitions
        NSLog(@"📋 Test 9: Bridge state transitions");
        knotapi.isBridgeActive = YES;
        knotapi.isClosing = NO;
        if ([knotapi isBridgeReadyForEvents]) {
            NSLog(@"✅ Bridge correctly reports ready when active and not closing");
        } else {
            NSLog(@"❌ Bridge incorrectly reports not ready when active and not closing");
            return 1;
        }
        
        // Test 10: Product name setting
        NSLog(@"📋 Test 10: Product name setting");
        knotapi.productName = @"test_product";
        if ([knotapi.productName isEqualToString:@"test_product"]) {
            NSLog(@"✅ Product name setting works correctly");
        } else {
            NSLog(@"❌ Product name setting failed");
            return 1;
        }
        
        NSLog(@"");
        NSLog(@"🎉 All Simple Event Tests Passed!");
        NSLog(@"");
        NSLog(@"📊 Test Summary:");
        NSLog(@"   ✅ Bridge initialization");
        NSLog(@"   ✅ Bridge state management");
        NSLog(@"   ✅ Dictionary safety");
        NSLog(@"   ✅ String safety");
        NSLog(@"   ✅ Metadata safety");
        NSLog(@"   ✅ Nil input handling");
        NSLog(@"   ✅ Non-dictionary input handling");
        NSLog(@"   ✅ Empty metadata handling");
        NSLog(@"   ✅ Bridge state transitions");
        NSLog(@"   ✅ Product name setting");
        NSLog(@"");
        NSLog(@"🔧 Event handling logic is working correctly!");
        
        return 0;
    }
}
