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

class HomeService {
  Future<UserProfile?> getUser({String? token}) async {
    try {
      print("Starting getUser method in HomeService");
      print("Token: $token");
      print("URL: ${ApiUrls.profileUrl}");

      final response = await commonApiService<Map<String, dynamic>>(
        method: "GET",
        url: ApiUrls.profileUrl,
        additionalHeaders: {
          'Authorization': 'Bearer $token',
        },
        fromJson: (json) => json as Map<String, dynamic>,
      );

      print("Raw API response: $response");

      if (response == null) {
        print("API response is null");
        showSnackbar("Failed to get a response from the server");
        return null;
      }

      if (response['success'] != true) {
        print("API call was not successful");
        showSnackbar(response['message'] ?? "API call was not successful");
        return null;
      }

      if (response['myData'] == null) {
        print("myData is null in the response");
        showSnackbar("User data not found in the response");
        return null;
      }

      print("Attempting to create UserModel from myData");
      final userModel = UserProfile.fromJson(response['myData']);
      print("UserModel created successfully: $userModel");
      return userModel;
    } catch (e) {
      print("Error in getUser: ${e.toString()}");
      showSnackbar("Error fetching user data: ${e.toString()}");
      return null;
    }
  }

// ... keep the fcmToken method as it was
}