import 'package:cipher_guard_sdk/src/domain/entities/rsa_key_pair.dart';
import 'package:cipher_guard_sdk/src/domain/entities/session_key.dart';
import 'package:cipher_guard_sdk/src/domain/entities/encrypted_message.dart';
import 'package:cipher_guard_sdk/src/domain/repositories/encryption_repository.dart';
import '../datasources/encryption_flutter_service.dart';

/// 加密 Repository 實作
/// 實現 EncryptionRepository 介面
/// 使用 Flutter 本地加密服務，無需原生平台
class EncryptionRepositoryImpl implements EncryptionRepository {
  final EncryptionFlutterService _service;

  const EncryptionRepositoryImpl(this._service);

  // ==================== RSA 金鑰管理 ====================

  @override
  Future<RsaKeyPair> generateRsaKeyPair({int keySize = 1024}) async {
    // 在 Isolate 中运行，避免阻塞主线程（1024-bit 约 150ms）
    final result = await _service.generateRsaKeyPairAsync(keySize: keySize);
    return RsaKeyPair(
      publicKey: result.publicKey,
      privateKey: result.privateKey,
    );
  }

  @override
  Future<String> encryptPrivateKey({
    required String privateKey,
    required String password,
  }) async {
    return _service.encryptPrivateKey(
      privateKey: privateKey,
      password: password,
    );
  }

  @override
  Future<String> decryptPrivateKey({
    required String encryptedPrivateKey,
    required String password,
  }) async {
    return _service.decryptPrivateKey(
      encryptedPrivateKey: encryptedPrivateKey,
      password: password,
    );
  }

  // ==================== 會話金鑰管理 ====================

  @override
  Future<SessionKey> generateSessionKey({int initialRound = 1}) async {
    final result = _service.generateSessionKey(initialRound: initialRound);
    return SessionKey(key: result.key, round: result.round);
  }

  @override
  Future<String> encryptSessionKey({
    required String sessionKey,
    required String publicKey,
  }) async {
    return _service.encryptSessionKey(
      sessionKey: sessionKey,
      publicKey: publicKey,
    );
  }

  @override
  Future<String> decryptSessionKey({
    required String encryptedSessionKey,
    required String privateKey,
  }) async {
    return _service.decryptSessionKey(
      encryptedSessionKey: encryptedSessionKey,
      privateKey: privateKey,
    );
  }

  // ==================== 訊息加解密 ====================

  @override
  Future<EncryptedMessage> encryptMessage({
    required String plaintext,
    required String sessionKey,
    required int round,
  }) async {
    final result = _service.encryptMessage(
      plaintext: plaintext,
      sessionKey: sessionKey,
      round: round,
    );
    return EncryptedMessage(round: result.round, data: result.data);
  }

  @override
  Future<String> decryptMessage({
    required String encryptedData,
    required String sessionKey,
    required int round,
  }) async {
    return _service.decryptMessage(
      encryptedData: encryptedData,
      sessionKey: sessionKey,
      round: round,
    );
  }

  // ==================== 缓存管理 ====================

  @override
  void clearCaches() {
    // No KDF cache — raw keys used for message encryption.
  }

  // ==================== 原生平台同步 ====================

  @override
  Future<void> syncEncryptionKeysToNative({
    required String chatId,
    required int activeRound,
    required int round,
    required String activeKey,
    required bool isSingle,
  }) async {
    // 同步功能仍由原生平台處理 (iOS App Group / Android SharedPreferences)
    // 此處保留接口，但實際同步透過 MethodChannel 完成
    // 注意：同步功能目前標記為保留
  }

  @override
  Future<void> syncAllEncryptionKeys({
    required Map<String, Map<String, dynamic>> chatMap,
  }) async {
    // 批量同步功能仍由原生平台處理
    // 此處保留接口，但實際同步透過 MethodChannel 完成
  }

  // ==================== 推送通知解密 ====================

  @override
  Future<void> setAesSecret({required String aesSecret}) async {
    _service.setAesSecret(aesSecret);
  }

  @override
  Future<String?> decryptPushNotification({
    required String encryptedData,
  }) async {
    return _service.decryptPushNotification(encryptedData: encryptedData);
  }
}
