abstract class TokenStorage {
  Future<String?> getAccessToken();
  Future<String?> getRefreshToken();
  Future<void> saveTokens({required String access, required String refresh});
  Future<void> clear();
}

class SecureTokenStorage implements TokenStorage {
  static const _accessTokenKey = 'access_token';
  static const _refreshTokenKey = 'refresh_token';

  final dynamic _storage;

  SecureTokenStorage([this._storage]);

  @override
  Future<String?> getAccessToken() async {
    return _storage?.read(key: _accessTokenKey);
  }

  @override
  Future<String?> getRefreshToken() async {
    return _storage?.read(key: _refreshTokenKey);
  }

  @override
  Future<void> saveTokens({
    required String access,
    required String refresh,
  }) async {
    await _storage?.write(key: _accessTokenKey, value: access);
    await _storage?.write(key: _refreshTokenKey, value: refresh);
  }

  @override
  Future<void> clear() async {
    await _storage?.delete(key: _accessTokenKey);
    await _storage?.delete(key: _refreshTokenKey);
  }
}
