import 'package:dio/dio.dart';

import '../models/get_invoice_model.dart';
import '../utils/common_api_service.dart';
import '../utils/show_snackbar.dart';

class GetSingleInvoiceService {
  Dio _dio = Dio();


  Future<GetInvoiceModel?> fetchInvoice(String token, String invoiceId) async {
    try {
      // Ensure the base URL and orderId are concatenated properly
      String url = "/api/pd-get-invoices/$invoiceId";

      final response = await commonApiService<GetInvoiceModel>(
        method: "GET",
        url: url,
        additionalHeaders: {
          'Authorization': 'Bearer $token',  // Correctly pass the token as 'Bearer <token>'
        },
        fromJson: (json) {
          // Pass the entire JSON response to the fromJson method of GetInvoiceModel
          return GetInvoiceModel.fromJson(json as Map<String, dynamic>);
        },
      );

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

// Future<GetInvoiceModel?> fetchInvoice(String token, String invoiceId) async {
  //   try {
  //     final response = await _dio.get(
  //       'https://api.cnapp.co.in/api/pd-get-invoices/$invoiceId',
  //       options: Options(
  //         headers: {
  //           'Authorization': 'Bearer $token',
  //         },
  //       ),
  //     );
  //
  //     // Check if the response data is not null and status code is 200
  //     if (response.statusCode == 200 && response.data != null) {
  //       return GetInvoiceModel.fromJson(response.data); // Parse the response
  //     } else {
  //       print('Invoice not found or server error');
  //       return null; // Return null if data is not as expected
  //     }
  //   } catch (e) {
  //     print('Error fetching invoice: $e');
  //     return null; // Return null if there is an error
  //   }
  // }
}
