import 'package:agrilink_vocpro/core/state/result_state.dart';
import 'package:agrilink_vocpro/domain/service/app_service.dart';
import 'package:flutter/foundation.dart';
import 'package:shared_preferences/shared_preferences.dart';

class SettingProvider extends ChangeNotifier {
  SettingProvider() {
    _init();
  }

  String userFullName = '';
  String userEmail = '';
  String userName = '';

  void _init() async {
    SharedPreferences prefs = await SharedPreferences.getInstance();
    userFullName = prefs.getString('fullName') ?? 'unknown';
    userEmail = prefs.getString('email') ?? 'unknown';
    userName = prefs.getString('username') ?? 'unknown';
    notifyListeners();
  }

  ResultState logoutState = ResultState.initial;
  Future<void> logout() async {
    logoutState = ResultState.loading;
    notifyListeners();
    try {
      final result = await AppService().logout();
      if (result.data == null) {
        logoutState = ResultState.hasData;
      } else {
        logoutState = ResultState.error;
      }
    } catch (e) {
      if (kDebugMode) {
        print('Error logout: $e');
      }
      logoutState = ResultState.error;
    }
    notifyListeners();
  }
}
