import 'package:flutter_secure_storage/flutter_secure_storage.dart';

/// 令牌安全存储（Keychain / EncryptedSharedPreferences）。
///
/// 存储策略：
///  - accessToken：同时存内存与加密存储（掉线即失，需重新登录时可读取恢复）；
///  - refreshToken：仅存加密存储（一次性，ses_ 前缀）。
class TokenStorage {
  const TokenStorage();

  static const _storage = FlutterSecureStorage();

  static const _kAccess = 'ttstd.accessToken';
  static const _kRefresh = 'ttstd.refreshToken';
  static const _kUsername = 'ttstd.username';

  Future<String?> readAccess() => _storage.read(key: _kAccess);
  Future<String?> readRefresh() => _storage.read(key: _kRefresh);
  Future<String?> readUsername() => _storage.read(key: _kUsername);

  Future<void> writeAccess(String token) =>
      _storage.write(key: _kAccess, value: token);
  Future<void> writeRefresh(String token) =>
      _storage.write(key: _kRefresh, value: token);
  Future<void> writeUsername(String username) =>
      _storage.write(key: _kUsername, value: username);

  Future<void> clear() async {
    await _storage.delete(key: _kAccess);
    await _storage.delete(key: _kRefresh);
    await _storage.delete(key: _kUsername);
  }
}
