import 'package:flutter/material.dart';
import 'package:get/get.dart';
import '../../../core/storage/secure_storage.dart';
import '../../../app/routes/app_pages.dart';

class SettingsController extends GetxController {
  final SecureStorage _storage = SecureStorage();

  var isDarkMode = false.obs;
  var pushEnabled = true.obs;
  var userName = ''.obs;
  var userPhone = ''.obs;
  var userRole = ''.obs;

  String get roleName {
    switch (userRole.value) {
      case 'super_admin': return 'مدير النظام';
      case 'admin': return 'مدير مكتب';
      case 'accountant': return 'محاسب';
      case 'viewer': return 'مشاهد';
      default: return 'مستخدم';
    }
  }

  @override
  void onInit() {
    super.onInit();
    _loadUserData();
    isDarkMode.value = Get.isDarkMode;
  }

  Future<void> _loadUserData() async {
    userName.value = await _storage.read('user_name') ?? '';
    userPhone.value = await _storage.read('user_phone') ?? '';
    userRole.value = await _storage.read('user_role') ?? '';
  }

  void toggleTheme() {
    isDarkMode.value = !isDarkMode.value;
    Get.changeThemeMode(isDarkMode.value ? ThemeMode.dark : ThemeMode.light);
  }

  void togglePush() {
    pushEnabled.value = !pushEnabled.value;
  }

  Future<void> logout() async {
    await _storage.clearAll();
    Get.offAllNamed(AppRoutes.PHONE_INPUT);
  }
}
