import 'package:cheminova/models/place_order_list_model.dart'; // Import your model
import 'package:cheminova/utils/api_urls.dart';
import '../utils/common_api_service.dart';
import '../utils/show_snackbar.dart';

class GetSingleOrderPlacedService {
  // Function to fetch a single placed order by its ID
  Future<PlacedOrderList?> getSinglePlacedOrder(String orderId) async {
    try {
      // Construct the API URL for the single placed order
      String url = "${ApiUrls.getSinglePlacedOrderUrl}$orderId";

      final response = await commonApiService<PlacedOrderList>(
        method: "GET",
        url: url,
        fromJson: (json) {
          // Check if the JSON response contains the 'singleOrder' key
          if (json['singleOrder'] != null) {
            // Convert the JSON response to a PlacedOrderList model
            return PlacedOrderList.fromJson(json['singleOrder'] as Map<String, dynamic>);
          } else {
            // Throw an exception if the order is not found in the response
            throw Exception("Order not found"); // Throw an exception if not found
          }
        },
      );

      return response; // Return the fetched order if successful
    } catch (e) {
      // Show an error message in a snackbar if an exception occurs
      showSnackbar(e.toString());
      return null; // Return null to indicate an error occurred
    }
  }
}
