import 'package:cheminova/models/user_model.dart';
import 'package:cheminova/utils/api_urls.dart';
import 'package:cheminova/utils/common_api_service.dart';
import 'package:cheminova/utils/show_snackbar.dart';

class HomeService {
  Future<UserModel?> getUser({String? token}) async {
    try {
      final response = await commonApiService<UserModel>(
        method: "GET",
        url: ApiUrls.profileUrl,
        additionalHeaders: {  // Pass the token here
          'Authorization': 'Bearer $token',
        },
        fromJson: (json) {
          if (json['user'] != null) {
            // Parse the user data from the API response
            return UserModel.fromJson(json['user']);
          }
          return json as UserModel;
        },
      );
      return response;
    } catch (e) {
      print(e.toString());
      showSnackbar(e.toString()); // Optional: show error using a Snackbar
      return null;
    }
  }


  Future<Map<String, dynamic>?> fcmToken(Map<String, dynamic> data,
      String? token) async {
    try {
      final response = await commonApiService<String>(
        url: ApiUrls.fcmUrl,
        method: 'POST',
        body: data,
        fromJson: (json) => json as String,
        // Just return the string response
        additionalHeaders: { // Pass the token here
          'Authorization': 'Bearer $token',
        },
      );

      if (response != null) {
        // Since the response is a string, wrap it in a Map to avoid type issues
        return {
          'message': response
        }; // Return the response in a map with 'message' as the key
      }
      return null;
    } catch (e) {
      showSnackbar(e.toString()); // Handle any errors
      return null;
    }
  }
}
