import 'package:flutter_secure_storage/flutter_secure_storage.dart';

class IAPPendingOrderService {
  static const _keyOrderNo = 'hi_iap_pending_order_no';
  static const _keyJwsData = 'hi_iap_pending_jws_data'; // 新增 Key

  // 创建统一存储实例
  static final _storage = FlutterSecureStorage(
    aOptions: const AndroidOptions(
      encryptedSharedPreferences: true, // Android 加密存储
    ),
    iOptions: const IOSOptions(
      accessibility: KeychainAccessibility.first_unlock, // iOS Keychain
    ),
  );

  /// 存储待处理订单号
  static Future<void> setPendingOrderNo(String orderNo) async {
    await _storage.write(key: _keyOrderNo, value: orderNo);
  }

  /// 获取待处理订单号
  static Future<String?> getPendingOrderNo() async {
    return await _storage.read(key: _keyOrderNo);
  }

  /// 存储 JWS 凭证数据
  static Future<void> setPendingJwsData(String jwsData) async {
    await _storage.write(key: _keyJwsData, value: jwsData);
  }

  /// 获取存储的 JWS 凭证数据
  static Future<String?> getPendingJwsData() async {
    return await _storage.read(key: _keyJwsData);
  }

  /// 清除待处理订单和 JWS 数据
  static Future<void> clearPendingOrderNo() async {
    await _storage.delete(key: _keyOrderNo);
    await _storage.delete(key: _keyJwsData);
  }
}
