import 'package:dio/dio.dart';
import 'package:flutter/material.dart';

import '../services/api_client.dart';
import '../services/api_urls.dart';

class ForgotPasswordProvider extends ChangeNotifier{

  final emailController = TextEditingController();
  bool _isLoading = false;
  bool get isLoading => _isLoading;
  final _apiClient = ApiClient();

  void setLoading(bool loading) {
    _isLoading = loading;
    notifyListeners();
  }
  Future<(bool, String)> forgotPassword() async {
    setLoading(true);
    try {
      Response response = await _apiClient.post(ApiUrls.forgotPasswordUrl,
          data: {'email': emailController.text.trim()});
      setLoading(false);
      if (response.statusCode == 200) {
        return (true, response.data['message'].toString());
      } else {
        return (false, response.data['message'].toString());
      }
    }
    on DioException catch (e) {
      setLoading(false);
      if (e.response != null && e.response?.data != null) {
        // Extracting the error message from the Dio response
        return (false, e.response!.data['message'].toString());
      } else {
        // When no response or response data is available
        return (false, 'Something went wrong');
      }
    }
    catch (e) {
      setLoading(false);
      return (false, 'Something want wrong');
    }
  }

}