import 'package:shared_preferences/shared_preferences.dart';

class BlockService {
  static final BlockService _instance = BlockService._internal();

  factory BlockService() {
    return _instance;
  }

  BlockService._internal();

  // Function to add a number to the blocked list
  Future<void> blockNumber(String number) async {
    if (number.isEmpty) return;

    final prefs = await SharedPreferences.getInstance();
    List<String> blockedNumbers = prefs.getStringList('blockedNumbers') ?? [];

    if (!blockedNumbers.contains(number)) {
      blockedNumbers.add(number);
      await prefs.setStringList('blockedNumbers', blockedNumbers);
      print('$number has been blocked');
    } else {
      print('$number is already blocked');
    }
  }

  // Function to remove a number from the blocked list
  Future<void> unblockNumber(String number) async {
    if (number.isEmpty) return;

    final prefs = await SharedPreferences.getInstance();
    List<String> blockedNumbers = prefs.getStringList('blockedNumbers') ?? [];

    if (blockedNumbers.contains(number)) {
      blockedNumbers.remove(number);
      await prefs.setStringList('blockedNumbers', blockedNumbers);
      print('$number has been unblocked');
    } else {
      print('$number is not blocked');
    }
  }

  // Check if a number is blocked
  Future<bool> isNumberBlocked(String number) async {
    if (number.isEmpty) return false;

    final prefs = await SharedPreferences.getInstance();
    List<String> blockedNumbers = prefs.getStringList('blockedNumbers') ?? [];
    return blockedNumbers.contains(number);
  }
}
