import 'package:flutter/material.dart';
import 'package:shared_preferences/shared_preferences.dart';
import '../../domain/entities/app_settings.dart';

/// Local datasource for persisting application settings via SharedPreferences.
class SettingsDatasource {
  final SharedPreferences _prefs;

  static const String _keyThemeMode = 'settings_theme_mode';
  static const String _keyNotifications = 'settings_notifications';
  static const String _keySound = 'settings_sound';
  static const String _keyHaptic = 'settings_haptic';
  static const String _keyTenantId = 'settings_tenant_id';
  static const String _keyTenantName = 'settings_tenant_name';
  static const String _keyLanguage = 'settings_language';
  static const String _keyBiometric = 'settings_biometric';
  static const String _keyTtsVoice = 'settings_tts_voice';
  static const String _keyTtsStyle = 'settings_tts_style';
  static const String _keyEngineType = 'settings_engine_type';

  SettingsDatasource(this._prefs);

  /// Loads settings from SharedPreferences.
  AppSettings loadSettings() {
    final themeModeIndex = _prefs.getInt(_keyThemeMode);
    return AppSettings(
      themeMode: themeModeIndex != null
          ? ThemeMode.values[themeModeIndex]
          : ThemeMode.dark,
      notificationsEnabled: _prefs.getBool(_keyNotifications) ?? true,
      soundEnabled: _prefs.getBool(_keySound) ?? true,
      hapticFeedback: _prefs.getBool(_keyHaptic) ?? true,
      selectedTenantId: _prefs.getString(_keyTenantId),
      selectedTenantName: _prefs.getString(_keyTenantName),
      language: _prefs.getString(_keyLanguage) ?? '',
      biometricEnabled: _prefs.getBool(_keyBiometric) ?? false,
      ttsVoice: _prefs.getString(_keyTtsVoice) ?? 'coral',
      ttsStyle: _prefs.getString(_keyTtsStyle) ?? '',
      engineType: _prefs.getString(_keyEngineType) ?? 'claude_agent_sdk',
    );
  }

  /// Saves settings to SharedPreferences.
  Future<void> saveSettings(AppSettings settings) async {
    await _prefs.setInt(_keyThemeMode, settings.themeMode.index);
    await _prefs.setBool(_keyNotifications, settings.notificationsEnabled);
    await _prefs.setBool(_keySound, settings.soundEnabled);
    await _prefs.setBool(_keyHaptic, settings.hapticFeedback);

    if (settings.selectedTenantId != null) {
      await _prefs.setString(_keyTenantId, settings.selectedTenantId!);
    } else {
      await _prefs.remove(_keyTenantId);
    }

    if (settings.selectedTenantName != null) {
      await _prefs.setString(_keyTenantName, settings.selectedTenantName!);
    } else {
      await _prefs.remove(_keyTenantName);
    }

    await _prefs.setString(_keyLanguage, settings.language);
    await _prefs.setBool(_keyBiometric, settings.biometricEnabled);
    await _prefs.setString(_keyTtsVoice, settings.ttsVoice);
    await _prefs.setString(_keyTtsStyle, settings.ttsStyle);
    await _prefs.setString(_keyEngineType, settings.engineType);
  }

  /// Removes all settings keys from SharedPreferences.
  Future<void> clearSettings() async {
    await _prefs.remove(_keyThemeMode);
    await _prefs.remove(_keyNotifications);
    await _prefs.remove(_keySound);
    await _prefs.remove(_keyHaptic);
    await _prefs.remove(_keyTenantId);
    await _prefs.remove(_keyTenantName);
    await _prefs.remove(_keyLanguage);
    await _prefs.remove(_keyBiometric);
    await _prefs.remove(_keyTtsVoice);
    await _prefs.remove(_keyTtsStyle);
    await _prefs.remove(_keyEngineType);
  }
}
