import 'package:sqflite/sqflite.dart'; import 'package:flutter/foundation.dart'; import 'local_database.dart'; class LocalPreference { static Future setSelectedCityId(int value) async { final db = await LocalDatabase().database; await db.insert( 'selected_city', { 'id': 1, 'city_id': value, }, conflictAlgorithm: ConflictAlgorithm.replace, ); } static Future getSelectedCityId() async { final db = await LocalDatabase().database; final result = await db.query( 'selected_city', where: 'id = ?', whereArgs: [1], ); if (result.isNotEmpty) { return result.first['city_id'] as int; } return 0; } /// Insert default onboarding row (call once in splash) static Future initOnboarding() async { final db = await LocalDatabase().database; final result = await db.query('onboarding_state'); if (result.isEmpty) { await db.insert( 'onboarding_state', { 'id': 1, 'is_first_time': 1, // true 'page': 0, }, ); } } /// Get onboarding page static Future getOnboardingPage() async { final db = await LocalDatabase().database; final result = await db.query( 'onboarding_state', where: 'id = ?', whereArgs: [1], ); if (result.isNotEmpty) { return result.first['page'] as int; } return 0; } /// Get isFirstTime value static Future isFirstTimeUser() async { final page = await getOnboardingPage(); return page < 3; } /// Move to next onboarding page static Future updateOnboardingPage(int page) async { final db = await LocalDatabase().database; await db.update( 'onboarding_state', { 'page': page, 'is_first_time': page < 3 ? 1 : 0, }, where: 'id = ?', whereArgs: [1], ); } /// Reset onboarding (for logout / testing) static Future resetOnboarding() async { await updateOnboardingPage(0); } /// Set login state static Future setLogin(bool value) async { final db = await LocalDatabase().database; await db.insert( 'login_state', { 'id': 1, 'is_logged_in': value ? 1 : 0, }, conflictAlgorithm: ConflictAlgorithm.replace, ); } /// Get login state static Future getLogin() async { final db = await LocalDatabase().database; final result = await db.query( 'login_state', where: 'id = ?', whereArgs: [1], ); if (result.isNotEmpty) { return result.first['is_logged_in'] == 1; } return false; } static Future clearLogin() async { final db = await LocalDatabase().database; await db.update( 'login_state', {'is_logged_in': 0}, where: 'id = ?', whereArgs: [1], ); } /// Set user tokens static Future setTokens({ required String accessToken, required String refreshToken, required int refreshTokenMaxAge, }) async { final db = await LocalDatabase().database; await db.insert( 'user_tokens', { 'id': 1, 'access_token': accessToken, 'refresh_token': refreshToken, 'refresh_token_max_age': refreshTokenMaxAge, }, conflictAlgorithm: ConflictAlgorithm.replace, ); } /// Update only access token (for refresh token flow) static Future setAccessToken(String accessToken) async { final db = await LocalDatabase().database; await db.update( 'user_tokens', {'access_token': accessToken}, where: 'id = ?', whereArgs: [1], ); } /// Get access token static Future getAccessToken() async { final db = await LocalDatabase().database; final result = await db.query( 'user_tokens', where: 'id = ?', whereArgs: [1], ); if (result.isNotEmpty) { return result.first['access_token'] as String?; } return null; } /// Get refresh token static Future getRefreshToken() async { final db = await LocalDatabase().database; final result = await db.query( 'user_tokens', where: 'id = ?', whereArgs: [1], ); if (result.isNotEmpty) { return result.first['refresh_token'] as String?; } return null; } /// Clear tokens (for logout) static Future clearTokens() async { final db = await LocalDatabase().database; await db.delete( 'user_tokens', where: 'id = ?', whereArgs: [1], ); } /// Set user details static Future setUserDetails({ required int userId, required String firstName, required String lastName, required String fullName, required String emailAddress, required String role, required int roleId, String? profileImage, // Added optional profileImage parameter }) async { final db = await LocalDatabase().database; await db.insert( 'user_details', { 'id': 1, 'user_id': userId, 'first_name': firstName, 'last_name': lastName, 'full_name': fullName, 'email_address': emailAddress, 'role': role, 'role_id': roleId, 'profile_image': profileImage, // Include profile image }, conflictAlgorithm: ConflictAlgorithm.replace, ); } /// Get userId static Future getUserId() async { final db = await LocalDatabase().database; final result = await db.query( 'user_details', where: 'id = ?', whereArgs: [1], ); if (result.isNotEmpty) { return result.first['user_id'] as int; } return null; } /// Set profile image with error handling static Future setProfileImage(String imageUrl) async { try { final db = await LocalDatabase().database; final result = await db.update( 'user_details', {'profile_image': imageUrl}, where: 'id = ?', whereArgs: [1], ); if (kDebugMode) { print('โœ… [LOCAL_PREF] Profile image saved: $imageUrl'); print('๐Ÿ“Š [LOCAL_PREF] Rows affected: $result'); } } catch (e) { if (kDebugMode) { print('โŒ [LOCAL_PREF] Error saving profile image: $e'); } rethrow; } } /// Get profile image static Future getProfileImage() async { try { final db = await LocalDatabase().database; final result = await db.query( 'user_details', columns: ['profile_image'], where: 'id = ?', whereArgs: [1], ); if (result.isNotEmpty) { final imageUrl = result.first['profile_image'] as String?; if (kDebugMode && imageUrl != null) { print('โœ… [LOCAL_PREF] Retrieved profile image: $imageUrl'); } return imageUrl; } return null; } catch (e) { if (kDebugMode) { print('โŒ [LOCAL_PREF] Error getting profile image: $e'); } return null; } } /// Set pass cart data static Future setPassCart({ required String cityName, required String heroImage, required String cardTypeName, required String cardDisplayName, required int themeColor, required int adultCount, required int childCount, required double adultPrice, required double childPrice, required int validityDuration, required double totalPrice, String? description, }) async { final db = await LocalDatabase().database; await db.insert( 'pass_cart', { 'id': 1, 'city_name': cityName, 'hero_image': heroImage, 'card_type_name': cardTypeName, 'card_display_name': cardDisplayName, 'theme_color': themeColor, 'adult_count': adultCount, 'child_count': childCount, 'adult_price': adultPrice, 'child_price': childPrice, 'validity_duration': validityDuration, 'total_price': totalPrice, 'description': description, }, conflictAlgorithm: ConflictAlgorithm.replace, ); if (kDebugMode) { print('โœ… [LOCAL_PREF] Pass cart saved: $cardDisplayName for $cityName'); } } /// Get pass cart data static Future?> getPassCart() async { try { final db = await LocalDatabase().database; final result = await db.query( 'pass_cart', where: 'id = ?', whereArgs: [1], ); if (result.isNotEmpty) { if (kDebugMode) { print('โœ… [LOCAL_PREF] Retrieved pass cart data'); } return result.first; } return null; } catch (e) { if (kDebugMode) { print('โŒ [LOCAL_PREF] Error getting pass cart: $e'); } return null; } } static Future clearPassCart() async { try { final db = await LocalDatabase().database; await db.delete( 'pass_cart', where: 'id = ?', whereArgs: [1], ); if (kDebugMode) { print('โœ… [LOCAL_PREF] Pass cart cleared'); } } catch (e) { if (kDebugMode) { print('โŒ [LOCAL_PREF] Error clearing pass cart: $e'); } } } static Future setSelectedCityLogo(String logoUrl) async { final db = await LocalDatabase().database; await db.update( 'selected_city', {'city_logo': logoUrl}, where: 'id = ?', whereArgs: [1], ); } static Future getSelectedCityLogo() async { final db = await LocalDatabase().database; final result = await db.query( 'selected_city', where: 'id = ?', whereArgs: [1], ); if (result.isNotEmpty) { return result.first['city_logo'] as String?; } return null; } static Future clearUserDetails() async { final db = await LocalDatabase().database; await db.update( 'user_details', { 'user_id': null, 'first_name': '', 'last_name': '', 'full_name': '', 'email_address': '', 'role': '', 'role_id': 0, 'profile_image': null, }, where: 'id = ?', whereArgs: [1], ); } static Future clearProfileImage() async { try { final db = await LocalDatabase().database; final result = await db.update( 'user_details', {'profile_image': null}, where: 'id = ?', whereArgs: [1], ); if (kDebugMode) { print('๐Ÿงน [LOCAL_PREF] Profile image cleared'); print('๐Ÿ“Š [LOCAL_PREF] Rows affected: $result'); } } catch (e) { if (kDebugMode) { print('โŒ [LOCAL_PREF] Error clearing profile image: $e'); } rethrow; } } static Future setLanguage(String languageCode) async { try { final db = await LocalDatabase().database; await db.insert( 'selected_language', { 'id': 1, 'language_code': languageCode, }, conflictAlgorithm: ConflictAlgorithm.replace, ); if (kDebugMode) { print('โœ… [LOCAL_PREF] Language saved: $languageCode'); } } catch (e) { if (kDebugMode) { print('โŒ [LOCAL_PREF] Error saving language: $e'); } rethrow; } } /// Get selected language code (defaults to 'en' if not set) /// Usage: TranslateLanguage.fromBcp47Code(await LocalPreference.getLanguage()) static Future getLanguage() async { try { final db = await LocalDatabase().database; final result = await db.query( 'selected_language', where: 'id = ?', whereArgs: [1], ); if (result.isNotEmpty) { final code = result.first['language_code'] as String; if (kDebugMode) { print('โœ… [LOCAL_PREF] Retrieved language: $code'); } return code; } return 'en'; // Default to English } catch (e) { if (kDebugMode) { print('โŒ [LOCAL_PREF] Error getting language: $e'); } return 'en'; } } static Future resetAppData() async { await clearLogin(); await clearTokens(); await clearUserDetails(); await clearPassCart();// optional await clearProfileImage();// optional await clearPassCart();// optional } static Future clearAllData() async { try { final db = await LocalDatabase().database; // Clear all tables await db.delete('selected_city'); await db.delete('login_state'); await db.delete('user_tokens'); await db.delete('user_details'); await db.delete('pass_cart'); if (kDebugMode) { print('๐Ÿงน [LOCAL_PREF] All local data cleared successfully'); } } catch (e) { if (kDebugMode) { print('โŒ [LOCAL_PREF] Error clearing all local data: $e'); } rethrow; } } }