registerUser method

Future<Map<String, dynamic>> registerUser({
  1. required String phoneNumber,
  2. required String otp,
  3. String? tenantRoleID,
  4. String? firstName,
  5. String? lastName,
  6. String? location,
  7. String? username,
  8. String? email,
  9. String? password,
  10. String? language,
  11. String? idProofPath,
  12. String? profilePhotoPath,
  13. String? videoPath,
})

Implementation

Future<Map<String, dynamic>> registerUser({
  required String phoneNumber,
  required String otp,
  String? tenantRoleID,
  String? firstName,
  String? lastName,
  String? location,
  String? username,
  String? email,
  String? password,
  String? language,
  String? idProofPath,
  String? profilePhotoPath,
  String? videoPath,
}) async {
  final phone = _normalizePhone(phoneNumber);

  final Map<String, dynamic> input = {
    "productToken": productToken,
    "phoneNumber": phone,
    "otp": otp,
    "firstName": firstName,
    "lastName": lastName,
    "location": location,
    "tenantRoleID": tenantRoleID,
    "username": username ?? "user_${DateTime.now().millisecondsSinceEpoch}",
    "email": email,
    "password": password,
    "language": language,
  };

  final fileFields = <String, String>{};

  if (idProofPath != null) fileFields["image"] = idProofPath;
  if (profilePhotoPath != null) fileFields["profile_photo"] = profilePhotoPath;
  if (videoPath != null) fileFields["video"] = videoPath;

  input.removeWhere((key, value) => value == null);

  final mutationName = tenantRoleID != null && tenantRoleID.isNotEmpty
      ? "registerTenantUserWithMembership"
      : "registerWithProductToken";

  final mutation = tenantRoleID != null && tenantRoleID.isNotEmpty
      ? """
    mutation RegisterTenantUserWithMembership(\$input: RegisterTenantUserInput!) {
      registerTenantUserWithMembership(input:\$input) {
        success
        message
        token
        tenantUser { id phoneNumber }
      }
    }
  """
      : """
    mutation RegisterWithProductToken(\$input: ProductTokenRegistrationInput!) {
      registerWithProductToken(input:\$input) {
        success
        message
        token
        tenantUser { id phoneNumber }
      }
    }
  """;

  try {
    Map<String, dynamic> res;

    if (fileFields.isEmpty) {
      res = await _api.graphql(mutation, {"input": input});
    } else {
      res = await _api.uploadMultipleFilesViaGraphQL(
        mutationName: mutationName,
        variables: {"input": input},
        fileFields: fileFields,
      );
    }

    Map<String, dynamic>? data =
        res["data"]?[mutationName] ?? res[mutationName] ?? res;

    if (data == null || data["success"] != true) {
      return {'success': false, 'message': data?['message'] ?? "Registration failed"};
    }

    final userId = data["tenantUser"]?["id"]?.toString();
    final token = data["token"];

    print("REGISTRATION RESPONSE:");
    print("Token: ${token != null ? 'Present' : ' Missing'}");
    print("UserId: $userId");
    print(" Full tenantUser: ${data["tenantUser"]}");

    if (token != null) {
      final role = tenantRoleID != null ? "employer" : "employee";

      //Save session and wait for completion
      await _saveSession(
        token,
        phone,
        userId: userId,
        role: role,
      );

      print("REGISTRATION SUCCESS - Session saved with userId: $userId");
    } else {
      print(" WARNING: Token is null, cannot save session");
    }

    return {
      'success': true,
      'userId': userId,
      'message': data["message"],
      'token': token,
    };

  } catch (e) {
    print("REGISTRATION ERROR: $e");
    return {'success': false, 'message': e.toString()};
  }
}