import 'dart:developer';
import 'package:shared_preferences/shared_preferences.dart';

class TokenManager {
  static const _tokenKey = 'token';
  static const _refreshTokenKey = 'refreshToken';
  static const _oneTimeFlagPrefix = 'one_time_flag__';

  static Future<void> setOneTimeFlag(
    String name, {
    required bool value,
  }) async {
    final prefs = await SharedPreferences.getInstance();
    await prefs.setBool('$_oneTimeFlagPrefix$name', value);
  }

  static Future<bool> getOneTimeFlag(String name) async {
    final prefs = await SharedPreferences.getInstance();
    return prefs.getBool('$_oneTimeFlagPrefix$name') ?? false;
  }

  static Future<void> clearOneTimeFlag(
    String name,
  ) async {
    final prefs = await SharedPreferences.getInstance();
    await prefs.remove('$_oneTimeFlagPrefix$name');
  }

  static Future<void> saveRefreshToken(String token) async {
    log('SAVE REFRESH TOKEN: $token');
    final prefs = await SharedPreferences.getInstance();
    await prefs.setString(_refreshTokenKey, token);
  }

  static Future<void> clearToken() async {
    final prefs = await SharedPreferences.getInstance();
    await prefs.setString(_refreshTokenKey, '');
    await prefs.setString(_tokenKey, '');
  }

  static Future<String?> getRefreshToken() async {
    final prefs = await SharedPreferences.getInstance();
    return prefs.getString(_refreshTokenKey);
  }

  static Future<void> removeToken() async {
    final prefs = await SharedPreferences.getInstance();
    await prefs.remove(_tokenKey);
  }
}
