/**
 * GrouplinkSdk.mm
 *
 * Implements the RNGroupLinkSDK TurboModule for both Old and New Architecture.
 *
 * Old Architecture: uses RCT_EXPORT_MODULE() + RCT_EXPORT_METHOD() macros.
 * New Architecture: conforms to NativeGrouplinkSdkSpec (generated by Codegen
 *                   from src/NativeGroupLinkSDK.ts) and exposes getTurboModule:.
 *
 * GroupLink SDK calls are routed through GrouplinkBridge (GrouplinkSdk.swift)
 * because GroupLink.xcframework only exports Swift ABI symbols — direct ObjC++
 *
 * NOTE: Delete ios/GrouplinkSdk.m after adding this file — both cannot coexist.
 */

#import <React/RCTBridgeModule.h>
#import "grouplink_sdk-Swift.h"

#ifdef RCT_NEW_ARCH_ENABLED
// #import "GrouplinkSdkSpec/GrouplinkSdkSpec.h"
#import "GrouplinkSdkSpec.h"
#endif

// In-memory store for the iOS notification name.
// GroupLinkSDK only exposes a setter, not a getter, so we cache it locally.
static NSString *_iosNotificationName = @"";

#ifdef RCT_NEW_ARCH_ENABLED

@interface RNGroupLinkSDK : NSObject <NativeGrouplinkSdkSpec>
@end

@implementation RNGroupLinkSDK

RCT_EXPORT_MODULE(RNGroupLinkSDK)

// ---------------------------------------------------------------------------
// iOS-only methods
// ---------------------------------------------------------------------------

- (void)startIOS:(NSString *)withToken {
  [GrouplinkBridge startIOSWithToken:withToken];
}

- (void)setDevicePushToken:(NSString *)withPushToken {
  [GrouplinkBridge setRemoteNotificationToken:withPushToken];
}

- (void)startBluetoothIOS {
  [GrouplinkBridge startBluetooth];
}

- (void)checkBluetoothIOS {
  [GrouplinkBridge checkBluetooth];
}

- (void)startLocationIOS {
  [GrouplinkBridge startLocation];
}

- (void)setRemoteIdIOS:(NSString *)remoteId {
  _iosNotificationName = remoteId;
  [GrouplinkBridge setRemoteIdIOS:remoteId];
}

- (void)setNotificationNameIOS:(NSString *)withNotificationName {
  _iosNotificationName = withNotificationName;
  [GrouplinkBridge setNotificationName:withNotificationName];
}

- (void)getUserID:(RCTPromiseResolveBlock)resolve
           reject:(RCTPromiseRejectBlock)reject {
  NSString *userId = [GrouplinkBridge getUserID];
  if (userId != nil) {
    resolve(userId);
  } else {
    reject(@"NO_USER_ID", @"Could not get userID at this moment", nil);
  }
}

- (void)getNotificationNameIOS:(RCTPromiseResolveBlock)resolve
                        reject:(RCTPromiseRejectBlock)reject {
  resolve(_iosNotificationName);
}

// ---------------------------------------------------------------------------
// Android-only methods — no-ops on iOS
// ---------------------------------------------------------------------------

- (void)getUserIdAndroid:(RCTPromiseResolveBlock)resolve
                  reject:(RCTPromiseRejectBlock)reject {
  reject(@"unsupported", @"getUserIdAndroid is Android only", nil);
}

- (void)startAndroid:(NSString *)token debbugable:(BOOL)debbugable {}

- (void)setFirebaseToken:(NSString *)token {}

- (void)requestFullIntentPermissionAndroid {}

- (void)setNotificationNameAndroid:(NSString *)notificationName {}

- (void)getNotificationNameAndroid:(RCTPromiseResolveBlock)resolve
                            reject:(RCTPromiseRejectBlock)reject {
  reject(@"unsupported", @"getNotificationNameAndroid is Android only", nil);
}

- (void)requestScheduleExactAlarm {}

// ---------------------------------------------------------------------------
// TurboModule bridge
// ---------------------------------------------------------------------------

- (std::shared_ptr<facebook::react::TurboModule>)getTurboModule:
    (const facebook::react::ObjCTurboModule::InitParams &)params {
  return std::make_shared<facebook::react::NativeGrouplinkSdkSpecJSI>(params);
}

@end

#else // Old Architecture -------------------------------------------------------

@interface RNGroupLinkSDK : NSObject <RCTBridgeModule>
@end

@implementation RNGroupLinkSDK

RCT_EXPORT_MODULE(RNGroupLinkSDK)

+ (BOOL)requiresMainQueueSetup {
  return YES;
}

// ---------------------------------------------------------------------------
// iOS-only methods
// ---------------------------------------------------------------------------

RCT_EXPORT_METHOD(startIOS:(NSString *)withToken) {
  [GrouplinkBridge startIOSWithToken:withToken];
}

RCT_EXPORT_METHOD(setDevicePushToken:(NSString *)withPushToken) {
  [GrouplinkBridge setRemoteNotificationToken:withPushToken];
}

RCT_EXPORT_METHOD(startBluetoothIOS) {
  [GrouplinkBridge startBluetooth];
}

RCT_EXPORT_METHOD(checkBluetoothIOS) {
  [GrouplinkBridge checkBluetooth];
}

RCT_EXPORT_METHOD(startLocationIOS) {
  [GrouplinkBridge startLocation];
}

RCT_EXPORT_METHOD(setRemoteIdIOS:(NSString *)remoteId) {
  [GrouplinkBridge setRemoteIdIOS:remoteId];
}

RCT_EXPORT_METHOD(setNotificationNameIOS:(NSString *)withNotificationName) {
  _iosNotificationName = withNotificationName;
  [GrouplinkBridge setNotificationName:withNotificationName];
}

RCT_EXPORT_METHOD(getUserID:(RCTPromiseResolveBlock)resolve
                  rejecter:(RCTPromiseRejectBlock)reject) {
  NSString *userId = [GrouplinkBridge getUserID];
  if (userId != nil) {
    resolve(userId);
  } else {
    reject(@"NO_USER_ID", @"Could not get userID at this moment", nil);
  }
}

RCT_EXPORT_METHOD(getNotificationNameIOS:(RCTPromiseResolveBlock)resolve
                  rejecter:(RCTPromiseRejectBlock)reject) {
  resolve(_iosNotificationName);
}

// ---------------------------------------------------------------------------
// Android-only methods — not registered on iOS in Old Architecture
// (JS guards with Platform.OS checks; not needed here)
// ---------------------------------------------------------------------------

@end

#endif // RCT_NEW_ARCH_ENABLED
