import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:flutter_secure_storage/flutter_secure_storage.dart';

const accessTokenKey = "access_token";
const refreshTokenKey = "refresh_token";

final tokenStorageRepositoryProvider = Provider<TokenStorageRepository>((ref) {
  return TokenStorageRepository();
});

// Secure storage backend for storing tokens in the browser
class TokenStorageRepository {
  static const _storage = FlutterSecureStorage();
  TokenStorageRepository();

  // Store the refresh token in the storage
  Future<void> storeRefreshToken(String token) async {
    await _storage.write(key: refreshTokenKey, value: token);
  }

  // Store the access token in the storage
  Future<void> storeAccessToken(String token) async {
    await _storage.write(key: accessTokenKey, value: token);
  }

  // Get the acccess token from the storage
  Future<String?> getAccessToken() async {
    final token = await _storage.read(key: accessTokenKey);
    return token;
  }

  // Get the refresh token from the storage
  Future<String?> getRefreshToken() async {
    final token = await _storage.read(key: refreshTokenKey);
    return token;
  }

  Future<void> clearStorage() async {
    await _storage.delete(key: refreshTokenKey);
    await _storage.delete(key: accessTokenKey);
  }
}
