import 'package:shared_preferences/shared_preferences.dart';

class ApiService {
  // This is the key use to save the URL in storage
  static const String _urlStorageKey = 'api_base_url';

  // This is default URL
  static const String _defaultUrl = 'https://dev9.pstw.com.my';

  // It starts as the default, but will be updated by init().
  static String baseUrl = _defaultUrl;

  /// Call this in main.dart BEFORE runApp() to load the saved URL.
  static Future<void> init() async {
    final prefs = await SharedPreferences.getInstance();
    // Get the saved URL. If it's not there, use the default.
    baseUrl = prefs.getString(_urlStorageKey) ?? _defaultUrl;
  }

  /// Call this from your settings dialog to save a new URL.
  static Future<void> setBaseUrl(String newUrl) async {
    final prefs = await SharedPreferences.getInstance();
    await prefs.setString(_urlStorageKey, newUrl);
    // Update the URL for the current app session
    baseUrl = newUrl;
  }
}