
#import "SnapmintCheckoutWrapper.h"
#import <UIKit/UIKit.h>
// auto-generated Swift header for the pod (pod name react-native-snapmint3 → react_native_snapmint3-Swift.h)
#if __has_include("react_native_snapmint3-Swift.h")
#import "react_native_snapmint3-Swift.h"
#else
#error "Swift bridge header not found. Ensure USE_FRAMEWORKS! is enabled and the pod is built."
#endif

@interface SnapmintCheckoutWrapper ()
@property(nonatomic,strong) NSURL *checkoutUrl;
@property(nonatomic,copy) SnapmintCompletionBlock completion;
@property(nonatomic,strong,nullable) NSDictionary *header;
@end

@implementation SnapmintCheckoutWrapper

- (instancetype)initWithUrl:(NSURL *)url completion:(SnapmintCompletionBlock)completion {
    if (self = [super init]) {
        _checkoutUrl = url;
        _completion = completion;
    }
    NSLog(@"[SnapmintWrapper] init(no header) url=%@", url.absoluteString);
    return self;
}

- (instancetype)initWithUrl:(NSURL *)url header:(NSDictionary * _Nullable)header completion:(SnapmintCompletionBlock)completion {
    if (self = [super init]) {
        _checkoutUrl = url;
        _completion = completion;
        _header = header;
    }
    NSLog(@"[SnapmintWrapper] init(with header) url=%@ headerKeys=%@", url.absoluteString, [header allKeys]);
    return self;
}

- (UIViewController *)createHostingController {
    if (!self.checkoutUrl) { return [UIViewController new]; }
    if (@available(iOS 13.0, *)) {
        __weak typeof(self) weakSelf = self;
        UIViewController *vc = nil;
        NSLog(@"[SnapmintWrapper] createHostingController headerPresent=%@", self.header ? @"YES" : @"NO");
        if (self.header) {
            vc = [SnapmintUIHostingControllerFactory hostingControllerFor:self.checkoutUrl
                                                                   header:self.header
                                                            resultHandler:^(id _Nullable result) {
                NSLog(@"🔥 [SnapmintWrapper] resultHandler CALLED (with header) - result=%@", result);
                if (!weakSelf.completion) { 
                    NSLog(@"⚠️ [SnapmintWrapper] completion block is nil, cannot forward");
                    return; 
                }
                NSLog(@"[SnapmintWrapper] result(with header) class=%@ payload=%@", result ? NSStringFromClass([result class]) : @"<nil>", result);
                NSString *status = [result valueForKey:@"status"] ?: @"unknown";
                NSString *message = [result valueForKey:@"message"] ?: @"No message";
                id dataVal = [result valueForKey:@"data"];
                NSMutableDictionary *resultDict = [@{ @"status": status,
                                                      @"message": message,
                                                      @"responseMsg": message,
                                                      @"timestamp": [NSDateFormatter localizedStringFromDate:[NSDate date]
                                                                                                   dateStyle:NSDateFormatterFullStyle
                                                                                                   timeStyle:NSDateFormatterFullStyle] } mutableCopy];
                if (dataVal) { resultDict[@"data"] = dataVal; }
                NSLog(@"[SnapmintWrapper] forwarding result status=%@ msgLen=%lu hasData=%@", status, (unsigned long)[message length], dataVal ? @"YES" : @"NO");
                weakSelf.completion(resultDict, nil);
            }];
        } else {
            vc = [SnapmintUIHostingControllerFactory hostingControllerFor:self.checkoutUrl
                                                            resultHandler:^(id _Nullable result) {
            NSLog(@"🔥 [SnapmintWrapper] resultHandler CALLED (no header) - result=%@", result);
            if (!weakSelf.completion) { 
                NSLog(@"⚠️ [SnapmintWrapper] completion block is nil, cannot forward");
                return; 
            }
            NSLog(@"[SnapmintWrapper] result(no header) class=%@ payload=%@", result ? NSStringFromClass([result class]) : @"<nil>", result);
            NSString *status = [result valueForKey:@"status"] ?: @"unknown";
            NSString *message = [result valueForKey:@"message"] ?: @"No message";
            id dataVal = [result valueForKey:@"data"];
            NSMutableDictionary *resultDict = [@{ @"status": status,
                                                  @"message": message,
                                                  @"responseMsg": message,
                                                  @"timestamp": [NSDateFormatter localizedStringFromDate:[NSDate date]
                                                                                               dateStyle:NSDateFormatterFullStyle
                                                                                               timeStyle:NSDateFormatterFullStyle] } mutableCopy];
            if (dataVal) { resultDict[@"data"] = dataVal; }
            NSLog(@"[SnapmintWrapper] forwarding result status=%@ msgLen=%lu hasData=%@", status, (unsigned long)[message length], dataVal ? @"YES" : @"NO");
            weakSelf.completion(resultDict, nil);
            }];
        }
        NSLog(@"[SnapmintWrapper] hostingController=%@", vc ? NSStringFromClass([vc class]) : @"<nil>");
        return vc ?: [UIViewController new];
    }
    // Fallback < iOS13
    if (self.completion) {
        self.completion(nil, @"iOS 13.0+ required for Snapmint checkout");
    }
    return [UIViewController new];
}

@end