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

class AppPreferences {
  static const String _keyDeviceId = 'device_id';
  static const String _keyParticipantId = 'participant_id';
  static const String _keyDisplayName = 'display_name';
  static const String _keyThemeMode = 'theme_mode'; // 'dark' or 'light'
  static const String _keyLanguage = 'language'; // 'en' or 'cs'
  static const String _keyHasPromptedName = 'has_prompted_name';

  static late SharedPreferences _prefs;

  static Future<void> init() async {
    _prefs = await SharedPreferences.getInstance();
    
    // Auto-generate ids if not present
    if (_prefs.getString(_keyDeviceId) == null) {
      final uuid = const Uuid().v4();
      await _prefs.setString(_keyDeviceId, uuid);
    }
    if (_prefs.getString(_keyParticipantId) == null) {
      final uuid = const Uuid().v4();
      await _prefs.setString(_keyParticipantId, uuid);
    }
    if (_prefs.getString(_keyDisplayName) == null) {
      final defaultName = 'Traveler_${const Uuid().v4().substring(0, 4)}';
      await _prefs.setString(_keyDisplayName, defaultName);
    }
  }

  static String get deviceId => _prefs.getString(_keyDeviceId)!;
  static String get participantId => _prefs.getString(_keyParticipantId)!;

  static String get displayName => _prefs.getString(_keyDisplayName)!;
  static Future<void> setDisplayName(String value) async {
    await _prefs.setString(_keyDisplayName, value);
  }

  static bool get hasPromptedName => _prefs.getBool(_keyHasPromptedName) ?? false;
  static Future<void> setHasPromptedName(bool value) async {
    await _prefs.setBool(_keyHasPromptedName, value);
  }

  static String get themeMode => _prefs.getString(_keyThemeMode) ?? 'dark';
  static Future<void> setThemeMode(String value) async {
    await _prefs.setString(_keyThemeMode, value);
  }

  static String get language => _prefs.getString(_keyLanguage) ?? 'en';
  static Future<void> setLanguage(String value) async {
    await _prefs.setString(_keyLanguage, value);
  }

  // Netlify integration
  static const String _keyNetlifyToken = 'netlify_token';
  
  static String? get netlifyToken => _prefs.getString(_keyNetlifyToken);
  static Future<void> setNetlifyToken(String? value) async {
    if (value == null) {
      await _prefs.remove(_keyNetlifyToken);
    } else {
      await _prefs.setString(_keyNetlifyToken, value.trim());
    }
  }

  static String? getNetlifySiteId(String tripId) {
    return _prefs.getString('netlify_site_id_$tripId');
  }
  static Future<void> setNetlifySiteId(String tripId, String? value) async {
    if (value == null) {
      await _prefs.remove('netlify_site_id_$tripId');
    } else {
      await _prefs.setString('netlify_site_id_$tripId', value);
    }
  }

  static String? getNetlifyUrl(String tripId) {
    return _prefs.getString('netlify_url_$tripId');
  }
  static Future<void> setNetlifyUrl(String tripId, String? value) async {
    if (value == null) {
      await _prefs.remove('netlify_url_$tripId');
    } else {
      await _prefs.setString('netlify_url_$tripId', value);
    }
  }
}
