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

class AuthLocalDataSourceImpl implements AuthLocalDataSource {
  final SharedPreferences sharedPreferences;

  static const String _accessTokenKey = 'access_token';
  static const String _refreshTokenKey = 'refresh_token';
  static const String _userIdKey = 'user_id';

  AuthLocalDataSourceImpl(this.sharedPreferences);

  @override
  Future<String?> getToken() async {
    return sharedPreferences.getString(_accessTokenKey);
  }

  @override
  String? getTokenSync() {
    return sharedPreferences.getString(_accessTokenKey);
  }

  Future<String?> getRefreshToken() async {
    return sharedPreferences.getString(_refreshTokenKey);
  }

  @override
  Future<void> saveToken(String token) async {
    // ignore: avoid_print
    print('[DEBUG] Saving new access token (starts with: ${token.substring(0, 10)}...)');
    await sharedPreferences.setString(_accessTokenKey, token);
  }

  Future<void> saveRefreshToken(String token) async {
    await sharedPreferences.setString(_refreshTokenKey, token);
  }

  @override
  Future<void> removeToken() async {
    await sharedPreferences.remove(_accessTokenKey);
    await sharedPreferences.remove(_refreshTokenKey);
  }

  @override
  Future<String?> getUserId() async {
    return sharedPreferences.getString(_userIdKey);
  }

  @override
  Future<void> saveUserId(String userId) async {
    await sharedPreferences.setString(_userIdKey, userId);
  }

  @override
  Future<void> removeUserId() async {
    await sharedPreferences.remove(_userIdKey);
  }
}
