import 'dart:convert';
import 'dart:math';

import 'package:flutter_secure_storage/flutter_secure_storage.dart';
import 'package:niogu_app/core/constants/app_key.dart';

class SecurityKeyManager {
  static const _storage = FlutterSecureStorage();

  static Future<void> getOrCreateDatabaseKey() async {
    final existing = await _storage.read(key: AppKey.DATABASE_ENCRYPTION);

    if (existing == null) {
      final random = Random.secure();

      final values = List<int>.generate(32, (i) => random.nextInt(256));

      await _storage.write(
        key: AppKey.DATABASE_ENCRYPTION,
        value: base64Url.encode(values),
      );
    }
  }
}
