import 'package:file_picker/file_picker.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:go_router/go_router.dart';
import 'package:http/http.dart' as http;
import 'package:intl/intl.dart';
import 'package:nhance_partner/data/services/auth_service.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'dart:convert';
import '../../data/utils/toastNotification.dart';
import '../../presentation/screens/staff/enquiry_single_page/model/dropdown_option.dart';
import '../../presentation/screens/staff/enquiry_single_page/model/enquiry_model.dart';
import '../config/env.dart';
import 'package:universal_html/html.dart' as html;
import 'package:http/http.dart' as http;

import '../config/navigation_service.dart';
import '../routing/app_router.dart';
import '../routing/routes.dart';

class ApiService {
  late final BuildContext context;
  String? _token;

  bool _isLoggingOut = false;

  Future<void> clearLocalStorageAndRedirect() async {
    if (_isLoggingOut) return;
    _isLoggingOut = true;

    await AuthService.clearToken();

    final ctx = navigatorKey.currentContext;
    if (ctx != null) {
      // ToastHelper.showErrorToast(ctx, 'Session expired');
      ctx.go(AppRoutes.login);
    } else {
      // fallback (web-safe)
      appRouter.go(AppRoutes.login);
    }
  }


  Future<void> _initializeToken() async {
    _token = await AuthService.getToken();
    print("APISERTOKEN - $_token");
  }

  // Future<void> clearLocalStorageAndRedirect() async {
  //   // final prefs = await SharedPreferences.getInstance();
  //   // await prefs.clear();
  //   // Assuming you have access to the context
  //   ToastHelper.showErrorToast(context, 'Session Out');
  //   // Navigator.pushNamed(context, 'login');
  //   AuthService.clearToken();
  //   context.go(AppRoutes.login);
  // }

  Future<Map<String, dynamic>> _makeGetRequest(
      Uri url,
      Map<String, String> headers,
      ) async {
    final response = await http.get(url, headers: headers);
    return _handleResponse(response);
  }

  Future<http.Response> _makeGethttpRequest(
    Uri url,
    Map<String, String> headers,
  ) async {
    final response = await http.get(url, headers: headers);

    if (response.statusCode == 401 || response.statusCode == 403) {
      await clearLocalStorageAndRedirect();
    }
    return response;
  }

  Future<Map<String, dynamic>> _makePostRequestJson(
    Uri url,
    Map<String, dynamic> body,
    Map<String, String> headers,
  ) async {
    final response = await http.post(
      url,
      headers: headers,
      body: jsonEncode(body), // ✅ convert to JSON
    );
    return _handleResponse(response);
  }

  // --------------------------- COMMON FILES __________________________________________

  Future<Map<String, dynamic>> createUserData(data, path) async {
    // print('path - $path');
    final url = Uri.parse('${Env.apiUrl}$path');

    if (_token == null) {
      await _initializeToken();
    }

    // print("data-------  $data}");

    final headers = {
      'Authorization': 'Bearer $_token' ?? '',
      'Content-Type': 'application/json',
      'app-signature': Env.App_Signature,
    };

    final response = await _makePostRequestJson(url, data, headers);
    return response;
  }

  Future<Map<String, dynamic>> fetchUploadEnquiryFiles(dynamic enqId) async {
    // print(_token);
    if (_token == null) {
      await _initializeToken();
    }

    final url = Uri.parse(
      '${Env.apiUrl}enquiry/enquiryFiles?enquiry_id=$enqId',
    );

    final headers = {
      'Authorization': 'Bearer $_token' ?? '',
      'app-signature': Env.App_Signature,
    };
    final response = await _makeGetRequest(url, headers);
    return response;
  }

  Future<Map<String, dynamic>> logoutUsingAPI(BuildContext context) async {
    // print(_token);
    if (_token == null) {
      await _initializeToken();
    }

    final url = Uri.parse('${Env.apiUrl}auth/logout');

    final headers = {
      'Authorization': 'Bearer $_token' ?? '',
      'app-signature': Env.App_Signature,
    };
    final response = await _makeGetRequest(url, headers);
    return response;
  }

  // Future<Map<String, dynamic>> _handleResponse(http.Response response) async {
  //   if (response.statusCode == 200) {
  //     return jsonDecode(response.body);
  //   } else if (response.statusCode == 401 || response.statusCode == 403) {
  //     await clearLocalStorageAndRedirect();
  //     return {};
  //   } else {
  //     throw Exception('Failed to load data');
  //   }
  // }

  Future<Map<String, dynamic>> _handleResponse(http.Response response) async {
    Map<String, dynamic> body = {};

    try {
      body = jsonDecode(response.body);
    } catch (_) {}

    if (response.statusCode == 200) {
      return body;
    }

    if (response.statusCode == 401 || response.statusCode == 403) {
      await clearLocalStorageAndRedirect();
      throw Exception('Session expired');
    }

    throw Exception('Server Error: ${response.statusCode}');
  }


  Future<Map<String, dynamic>> softDeletePolicyApi(String policyId) async {
    // Ensure token is available
    if (_token == null) {
      await _initializeToken();
    }

    // Define the endpoint with query parameter
    final url = Uri.parse('${Env.apiUrl}policy/softdelete?policy_id=$policyId');

    final headers = {
      'Authorization': 'Bearer $_token', // Removed null-coalesce as _token is checked above
      'app-signature': Env.App_Signature,
    };

    // Using your standard helper for consistency and error handling
    final response = await _makeGetRequest(url, headers);

    return response;
  }

  Future<Map<String, dynamic>> CheckDuplicate(
    BuildContext context,
    String value,
  ) async {
    if (_token == null) {
      await _initializeToken();
    }

    String apiUrldata;

    final response = await http.get(
      Uri.parse('${Env.apiUrl}enquiry/checkVehicleDuplicate?reg_no=$value'),
      headers: {
        'Authorization': 'Bearer $_token' ?? '',
        'app-signature': Env.App_Signature,
      },
    );

    if (response.statusCode == 200) {
      try {
        final data = json.decode(response.body);

        // Updated to match actual response structure
        if (data['status'] == "exists") {
          final msg = data['message'] ?? "Already exists";
          final field = data['field'] ?? "";

          // print("$field - $value : $msg");
          // print("EXIST REG");

          return {"message": msg, "field": field};
        } else {
          print("NOT EXIST REG");
          // print("$value : ${data['message'] ?? "is a new value"}");
          return {};
        }
      } catch (e) {
        throw Exception('Error parsing response: $e');
      }
    } else if (response.statusCode == 403) {
      await clearLocalStorageAndRedirect();
      return {};
    } else {
      throw Exception(
        'Failed to load checkDuplicate data. Status code: ${response.statusCode}',
      );
    }
  }

  Future<void> getPdfDownload1(path, id) async {

    print('GetDownload');

    final url = Uri.parse('${Env.apiUrl}$path');
    print('GetDownload - $url');
    await _initializeToken();

    if (_token == null) {
      throw Exception('Token not found. Please log in.');
    }

    final headers = {
      'Authorization': 'Bearer $_token' ?? '',
      'app-signature': Env.App_Signature,
    };
    final response = await _makeGethttpRequest(url, headers);

    if (response.statusCode == 200) {
      try {
        print("PDf Dowloaded");

        // Create a blob from the response body
        final blob = html.Blob([response.bodyBytes]);

        // Generate a download URL for the blob
        final url = html.Url.createObjectUrlFromBlob(blob);

        // Create a link element to trigger the download
        final anchor = html.AnchorElement(href: url)
          ..setAttribute('download', 'nhancePartner_$id.pdf')
          ..click();

        // Revoke the download URL to free up resources
        html.Url.revokeObjectUrl(url);
      } catch (e) {
        throw Exception('Error parsing response: $e');
      }
    } else if (response.statusCode == 404) {
      showDialog(
        context: context,
        builder: (BuildContext context) {
          return AlertDialog(
            title: Text('File not found.'),
            // content: Text('File not found.'),
            actions: [
              TextButton(
                child: Text('OK'),
                onPressed: () {
                  Navigator.of(context).pop(); // Close the dialog
                },
              ),
            ],
          );
        },
      );
    } else {
      throw Exception('Failed to load plans');
    }
  }

  String extractFileName(String? contentDisposition, String fallback) {
    if (contentDisposition == null) return fallback;

    // Check for extended filename first (RFC 5987)
    final filenameStarMatch = RegExp(
      r'filename\*\s*=\s*([^;]+)',
    ).firstMatch(contentDisposition);
    if (filenameStarMatch != null) {
      String value = filenameStarMatch.group(1)!;
      // Remove UTF-8'' prefix if exists
      value = value.replaceAll(RegExp(r"UTF-8''"), '');
      return Uri.decodeFull(value.replaceAll('"', ''));
    }

    // Fallback to regular filename=
    final filenameMatch = RegExp(
      r'filename\s*=\s*"?([^";]+)"?',
    ).firstMatch(contentDisposition);
    if (filenameMatch != null) {
      return filenameMatch.group(1)!;
    }

    // Fallback: use the provided fallback string
    return fallback;
  }

  Future<void> getPdfDownload(
    // BuildContext context,
    String path,
    String id,
  ) async {
    print('Sa - getPdfDownload - $path');

    // if (path.isEmpty) {
    //   ToastHelper.showInfoToast(context, 'No file path found.');
    //   return;
    // }

    final url = Uri.parse('${Env.apiUrl}$path');
    // final url = Uri.parse('${Env.apiUrl}$path');
    print('Sb - getPdfDownload - $url');
    await _initializeToken();

    if (_token == null) throw Exception('Token not found. Please log in.');

    final headers = {
      'Authorization': 'Bearer $_token',
      // 'App-Signature': Env.App_Signature,
      'app-signature': Env.App_Signature,
    };

    final response = await _makeGethttpRequest(url, headers);

    if (response.statusCode == 200) {
      final contentType = response.headers['content-type'] ?? '';
      if (contentType.contains('application/json')) {
        final jsonResponse = jsonDecode(response.body);
        if (jsonResponse['code'] == 404 || jsonResponse['code'] == 500) {
          ToastHelper.show('File Not Found', Colors.red);
        } else {
          ToastHelper.show('No File Found', Colors.red);
        }
        return;
      } else {
        final blob = html.Blob([response.bodyBytes]);
        final blobUrl = html.Url.createObjectUrlFromBlob(blob);
        print('Sc - getPdfDownload - $blobUrl');
        // --- filename resolution ---
        // String fileName = 'download_$id';
        // print('getPdfDownloadfileName $fileName');
        // final contentDisp = response.headers['content-disposition'];
        // print('getPdfDownloadcontentDisp  $contentDisp');
        // if (contentDisp != null && contentDisp.contains('filename=')) {
        //   print('getPdfDownloadcontentDisp..1');
        //   fileName = contentDisp.split('filename=')[1].replaceAll('"', '');
        //   print('getPdfDownloadcontentDisp..2');
        // } else {
        //   print('getPdfDownloadcontentDisp..3');
        //   // fallback: URL or id
        //   fileName = path.split('/').last;
        //   if (id != null) fileName = '${id}_$fileName';
        // }

        final contentDisp = response.headers['content-disposition'];
        print('Sd getPdfDownloadcontentDisp  $contentDisp');

        String fileName = extractFileName(contentDisp, path.split('/').last);
        print('Se Resolved fileName: $fileName');
        final anchor = html.AnchorElement(href: blobUrl)
          ..setAttribute('download', fileName)
          ..click();

        html.Url.revokeObjectUrl(blobUrl);
      }
    } else if (response.statusCode == 404) {
      showDialog(
        context: context,
        builder: (context) => AlertDialog(
          title: const Text('File not found.'),
          actions: [
            TextButton(
              child: const Text('OK'),
              onPressed: () => Navigator.of(context).pop(),
            ),
          ],
        ),
      );
    } else if (response.statusCode == 403) {
      await clearLocalStorageAndRedirect();
    } else if (response.statusCode == 500) {
      ToastHelper.show('No File Found', Colors.red);
    } else if (response.statusCode == 302) {
      print('Sf getPdfDownload 3-PP-302');
      ToastHelper.show('No File Found', Colors.red);
    } else {
      ToastHelper.show('No File Found', Colors.red);
      throw Exception('Failed to download file');
    }
  }

  Future<void> generateChartExcel(
    // BuildContext context,
    String path,
    String id,
    String month,
    dynamic managerId,
       {DateTime? toDate, DateTime? fromDate}
  ) async {
    print('Sq getxl 1 - $path');
    print('Sq toDate - $toDate');
    print('Sq fromDate - $fromDate');

    dynamic pathVal;
    // if (path == 'Insurer') {
    //   pathVal =
    //       'dashboard/downloadInsurerPoliciesExcel?manager_id=$managerId&month=$month&insurer_id=$id';
    // } else if (path == 'Broker') {
    //   pathVal =
    //       'dashboard/downloadBrokerPoliciesExcel?manager_id=$managerId&month=$month&broker_id=$id';
    // } else if (path == 'Product') {
    //   pathVal =
    //       'dashboard/downloadProductPoliciesExcel?manager_id=$managerId&month=$month&vehicle_type=$id';
    // } else if (path == 'PerformingTop50') {
    //   pathVal =
    //       'dashboard/downloadAgentMonthlyPoliciesExcel?manager_id=$managerId&agent_id=$id';
    // } else if (path == 'PerformingAgentAll50') {
    //   pathVal = 'dashboard/downloadT50AgentPoliciesExcel?manager_id=$managerId';
    // } else if (path == 'NonPerformingBelow50K') {
    //   pathVal = 'dashboard/downloadLowPremiumAgentExcel?manager_id=$managerId';
    // } else if (path == 'staff_pending_summary') {
    //   pathVal = 'dashboard/downloadStaffPendingSummaryExcel?manager_id=$managerId';
    // } else if (path == 'staff_pending_summary_by_id') {
    //   pathVal = 'dashboard/downloadStaffPendingSummaryExcelByID?manager_id=$managerId&staff_id=$id&status=$month';
    // } else {
    // //NoBusiness
    //   pathVal =
    //       'dashboard/downloadAgentsWithoutPoliciesExcel?manager_id=$managerId';
    // }

    if (path == 'Insurer') {
      pathVal =
      'dashboard/downloadExcelInsurer?manager_id=$managerId&month=$month&insurer_id=$id';
    } else if (path == 'Broker') {
      pathVal =
      'dashboard/downloadExcelBroker?manager_id=$managerId&month=$month&broker_id=$id';
    } else if (path == 'Product') {
      pathVal =
      'dashboard/downloadExcelProduct?manager_id=$managerId&month=$month&vehicle_type=$id';
    } else if (path == 'PerformingTop50') {
      pathVal = 'dashboard/downloadExcelPerformingAgentsTop50?manager_id=$managerId';
    } else if (path == 'PerformingAgentsByID') {
      pathVal =
      'dashboard/downloadExcelPerformingAgentsByID?manager_id=$managerId&agent_id=$id';
    } else if (path == 'NonPerformingBelow50K') {
      pathVal = 'dashboard/downloadExcelLowPremium?manager_id=$managerId';
    } else if (path == 'staff_pending_summary') {
      pathVal = 'dashboard/downloadExcelStaffPendingSummary?manager_id=$managerId';
    } else if (path == 'staff_pending_summary_by_id') {
      pathVal = 'dashboard/downloadExcelStaffPendingSummaryByID?manager_id=$managerId&staff_id=$id&status=$month';
    } else {
      //NoBusiness
      pathVal =
      'dashboard/downloadExcelWithoutPolicies?manager_id=$managerId';
    }

    if (fromDate != null && toDate != null) {
      final from = DateFormat('dd-MM-yyyy').format(fromDate);
      final to = DateFormat('dd-MM-yyyy').format(toDate);
      pathVal += '&from_date=$from&to_date=$to';
    }
    // print('Final Excel URL => $pathVal');

    final url = Uri.parse('${Env.apiUrl}$pathVal'); // original
    print('sq1 2 - $url');
    await _initializeToken();

    if (_token == null) throw Exception('Token not found. Please log in.');

    final headers = {
      'Authorization': 'Bearer $_token',
      // 'App-Signature': Env.App_Signature,
      'app-signature': Env.App_Signature,
    };

    final response = await _makeGethttpRequest(url, headers);

    if (response.statusCode == 200) {
      final contentType = response.headers['content-type'] ?? '';
      if (contentType.contains('application/json')) {
        final jsonResponse = jsonDecode(response.body);
        if (jsonResponse['code'] == 404 || jsonResponse['code'] == 500) {
          ToastHelper.show('File Not Found', Colors.red);
        } else {
          ToastHelper.show('No File Found', Colors.red);
        }
        return;
      } else {
        final blob = html.Blob([response.bodyBytes]);
        final blobUrl = html.Url.createObjectUrlFromBlob(blob);
        print('sq1 3 $blobUrl');
        final contentDisp = response.headers['content-disposition'];
        print('getPdfDownloadcontentDisp  $contentDisp');

        String fileName = extractFileName(contentDisp, path.split('/').last);
        print('sq 4 Resolved fileName: $fileName');
        final anchor = html.AnchorElement(href: blobUrl)
          ..setAttribute('download', fileName)
          ..click();

        html.Url.revokeObjectUrl(blobUrl);
      }
    } else if (response.statusCode == 404) {
      showDialog(
        context: context,
        builder: (context) => AlertDialog(
          title: const Text('File not found.'),
          actions: [
            TextButton(
              child: const Text('OK'),
              onPressed: () => Navigator.of(context).pop(),
            ),
          ],
        ),
      );
    } else if (response.statusCode == 403) {
      await clearLocalStorageAndRedirect();
    } else if (response.statusCode == 500) {
      ToastHelper.show('No File Found', Colors.red);
    } else if (response.statusCode == 302) {
      print('sq 3-PP-302');
      ToastHelper.show('No File Found', Colors.red);
    } else {
      ToastHelper.show('No File Found', Colors.red);
      throw Exception('Failed to download file');
    }
  }

  Future<void> generatePerformanceDashboardExcel(
    // BuildContext context,
    String path,
    String id,
    String salesId,
    String month,
    dynamic managerId,
  ) async {
    print('sm  1 - $path');

    dynamic pathVal;

    pathVal =
        'dashboard/downloadExcelStaffAndProduct?manager_id=$managerId&month=$month&sales_executive_id=$salesId&vehicle_type=$id';

    final url = Uri.parse('${Env.apiUrl}$pathVal');

    print('sm 2 - $url');
    await _initializeToken();

    if (_token == null) throw Exception('Token not found. Please log in.');

    final headers = {
      'Authorization': 'Bearer $_token',
      // 'App-Signature': Env.App_Signature,
      'app-signature': Env.App_Signature,
    };

    final response = await _makeGethttpRequest(url, headers);

    if (response.statusCode == 200) {
      final contentType = response.headers['content-type'] ?? '';
      if (contentType.contains('application/json')) {
        final jsonResponse = jsonDecode(response.body);
        if (jsonResponse['code'] == 404 || jsonResponse['code'] == 500) {
          ToastHelper.show('File Not Found', Colors.red);
        } else {
          ToastHelper.show('No File Found', Colors.red);
        }
        return;
      } else {
        final blob = html.Blob([response.bodyBytes]);
        final blobUrl = html.Url.createObjectUrlFromBlob(blob);
        print('sm 3 $blobUrl');
        final contentDisp = response.headers['content-disposition'];
        print('sm4   $contentDisp');

        String fileName = extractFileName(contentDisp, path.split('/').last);
        print('sm 5 Resolved fileName: $fileName');
        final anchor = html.AnchorElement(href: blobUrl)
          ..setAttribute('download', fileName)
          ..click();

        html.Url.revokeObjectUrl(blobUrl);
      }
    } else if (response.statusCode == 404) {
      showDialog(
        context: context,
        builder: (context) => AlertDialog(
          title: const Text('File not found.'),
          actions: [
            TextButton(
              child: const Text('OK'),
              onPressed: () => Navigator.of(context).pop(),
            ),
          ],
        ),
      );
    } else if (response.statusCode == 403) {
      await clearLocalStorageAndRedirect();
    } else if (response.statusCode == 500) {
      ToastHelper.show('No File Found', Colors.red);
    } else if (response.statusCode == 302) {
      print('smno 3-PP-302');
      ToastHelper.show('No File Found', Colors.red);
    } else {
      ToastHelper.show('No File Found', Colors.red);
      throw Exception('Failed to download file');
    }
  }

  Future<void> downloadFile({
    required String? apiUrl,
    required PlatformFile? localFile,
    required String? fileName,
    String? apiId, // optional if your API needs it
  }) async {
    try {
      if (localFile != null) {
        if (kIsWeb) {
          final blob = html.Blob([localFile.bytes!]);
          final url = html.Url.createObjectUrlFromBlob(blob);

          final anchor = html.AnchorElement(href: url)
            ..setAttribute('download', localFile.name)
            ..click();

          html.Url.revokeObjectUrl(url);
          print("Download triggered successfully (web)!");
        } else {
          print("Local file path: ${localFile.path}");
          // await OpenFilex.open(localFile.path!);
        }
      } else if (apiUrl != null) {
        print("Download from API: $apiUrl");
        print("Download from API: $apiId!");
        await getPdfDownload(apiUrl, apiId!);
      } else {
        print("⚠️ No file available to download");
      }
    } catch (e) {
      print("Error during download: $e");
    }
  }

  Future<Map<String, dynamic>> updateStatus(id, status, role) async {
    print('updateStatusupdateStatus');
    final Map<String, dynamic> data = {
      "id": int.parse(id),
      "is_active": int.parse(status),
    };
    final url;

    if (role == 'salesExecutive') {
      url = Uri.parse('${Env.apiUrl}salesExecutive/changeExecutiveStatus');
    } else if (role == 'staff') {
      url = Uri.parse('${Env.apiUrl}staff/changeStaffStatus');
    } else {
      url = Uri.parse('${Env.apiUrl}agent/changeAgentStatus');
    }

    // final token = await getToken(); // Fetch token

    if (_token == null) {
      throw Exception('Token not found. Please log in.');
    }

    print("data-------  $data}");

    final headers = {
      'Authorization': 'Bearer $_token',
      'Content-Type': 'application/json',
      'app-signature': Env.App_Signature,
      // 'app-signature': 'nhance-partner-2025-signature-35468846JRhH551HK',
    };

    final response = await _makePostRequestJson(url, data, headers);
    return response;
  }

  Future<Map<String, dynamic>> updateStatusMasters(
    id,
    status,
    masterName,
    userId,
  ) async {
    print('updateStatusMaster $id $status $masterName $userId');
    final Map<String, dynamic> data = {
      "is_active": int.parse(status),
      "updated_by": userId,
    };
    final url;

    if (masterName == 'Broker') {
      url = Uri.parse('${Env.apiUrl}master/updateBrokerStatus/$id');
    }
    else if (masterName == 'EndorsementType') {
      url = Uri.parse('${Env.apiUrl}master/updateEndorsementStatus/$id');
    }
    else if (masterName == 'VehicleType') {
      url = Uri.parse('${Env.apiUrl}master/updateVehicleTypeStatus/$id');
    }
    else {
      url = Uri.parse('${Env.apiUrl}master/updatePaymentModeStatus/$id');
    }

    // final token = await getToken(); // Fetch token

    if (_token == null) {
      throw Exception('Token not found. Please log in.');
    }

    print("data-------  $data}");

    final headers = {
      'Authorization': 'Bearer $_token',
      'Content-Type': 'application/json',
      'app-signature': Env.App_Signature,
      // 'app-signature': 'nhance-partner-2025-signature-35468846JRhH551HK',
    };

    final response = await _makePostRequestJson(url, data, headers);
    return response;
  }

  // ----------------------------------- Staff Level Count -------------------------------------------------
  Future<Map<String, dynamic>> fetchStaffLevelCount(id, userId) async {
    // print(_token);
    if (_token == null) {
      await _initializeToken();
    }

    final url;

      url = Uri.parse('${Env.apiUrl}dashboard/managerDashboard?manager_id=$id&staff_id=$userId');

    final headers = {
      'Authorization': 'Bearer $_token' ?? '',
      'app-signature': Env.App_Signature,
    };

    final response = await _makeGetRequest(url, headers);
    return response;
  }

  // ----------------------------------- Dashboard -------------------------------------------------
  Future<Map<String, dynamic>> fetchDashboard(int id, role, userId) async {
    // print(_token);
    if (_token == null) {
      await _initializeToken();
    }

    final url;

    if (role == 'manager') {
      url = Uri.parse('${Env.apiUrl}dashboard/managerDashboard?manager_id=$id');
    } else if (role == 'handler') {
      url = Uri.parse(
        '${Env.apiUrl}dashboard/handlerDashboard?manager_id=$id&handler_id=$userId',
      );
    } else if (role == 'staff') {
      url = Uri.parse('${Env.apiUrl}dashboard/staffDashboard?staff_id=$userId');
    } else {
      url = Uri.parse('${Env.apiUrl}dashboard/agentDashboard?agent_id=$userId');
    }

    final headers = {
      'Authorization': 'Bearer $_token' ?? '',
      'app-signature': Env.App_Signature,
    };

    final response = await _makeGetRequest(url, headers);
    return response;
  }

  Future<Map<String, dynamic>> findBusinessDashboardData(id, broker) async {
    // print(_token);
    if (_token == null) {
      await _initializeToken();
    }
    final url = Uri.parse(
      '${Env.apiUrl}dashboard/businessDashboard?manager_id=$id',
    );

    final headers = {
      'Authorization': 'Bearer $_token' ?? '',
      'app-signature': Env.App_Signature,
    };
    final response = await _makeGetRequest(url, headers);
    return response;
  }

  Future<Map<String, dynamic>> findPartnerDashboardData(id, fromDate,toDate) async {
    // print(_token);
    if (_token == null) {
      await _initializeToken();
    }

    late Uri url;

    /// ✅ CASE 1: fromDate OR toDate is null → normal API
    if (fromDate == null || toDate == null) {
      url = Uri.parse(
        '${Env.apiUrl}dashboard/partnerDashboard?manager_id=$id',
      );
    }
    /// ✅ CASE 2: both dates present → filtered API
    else {
      final from = DateFormat('dd-MM-yyyy').format(fromDate);
      final to = DateFormat('dd-MM-yyyy').format(toDate);

      url = Uri.parse(
        '${Env.apiUrl}dashboard/partnerDashboard'
            '?manager_id=$id'
            '&from_date=$from'
            '&to_date=$to',
      );
    }

    final headers = {
      'Authorization': 'Bearer $_token' ?? '',
      'app-signature': Env.App_Signature,
    };
    final response = await _makeGetRequest(url, headers);
    return response;
  }

  Future<Map<String, dynamic>> findProductivityDashboardData(id, broker) async {
    // print(_token);
    if (_token == null) {
      await _initializeToken();
    }
    final url = Uri.parse(
      '${Env.apiUrl}dashboard/productivityDashboard?manager_id=$id',
    );

    final headers = {
      'Authorization': 'Bearer $_token' ?? '',
      'app-signature': Env.App_Signature,
    };
    final response = await _makeGetRequest(url, headers);
    return response;
  }

  // -------------------------------- AGENT ----------------------------------------------

  Future<Map<String, dynamic>> fetchAgentIncentiveList(agentId) async {
    // print(_token);
    if (_token == null) {
      await _initializeToken();
    }

    final url = Uri.parse(
      '${Env.apiUrl}agent/agentIncentiveFileList?agent_id=${agentId}',
    );


    final headers = {
      'Authorization': 'Bearer $_token' ?? '',
      'app-signature': Env.App_Signature,
    };
    final response = await _makeGetRequest(url, headers);
    return response;
  }

  Future<Map<String, dynamic>> fetchManagerIncentiveList(mangerId) async {
    // print(_token);
    if (_token == null) {
      await _initializeToken();
    }

    final url = Uri.parse(
      '${Env.apiUrl}staff/managerIncentiveFileList?manager_id=$mangerId',
    );

    final headers = {
      'Authorization': 'Bearer $_token' ?? '',
      'app-signature': Env.App_Signature,
    };
    final response = await _makeGetRequest(url, headers);
    return response;
  }

  Future<Map<String, dynamic>> fetchAgentUserList(int managerId) async {
    // print(_token);
    if (_token == null) {
      await _initializeToken();
    }

    final userId = '1';
    final url = Uri.parse(
      '${Env.apiUrl}agent/agentList?manager_id=${managerId}',
    );

    final headers = {
      'Authorization': 'Bearer $_token' ?? '',
      'app-signature': Env.App_Signature,
    };
    final response = await _makeGetRequest(url, headers);
    return response;
  }

  Future<Map<String, dynamic>> findSingleAgentData(id) async {
    // print(_token);
    if (_token == null) {
      await _initializeToken();
    }
    // final url = Uri.parse('${Env.apiUrl}agent/agentList');


    final url = Uri.parse('${Env.apiUrl}agent/findAgent?id=$id');
    final headers = {
      'Authorization': 'Bearer $_token' ?? '',
      'app-signature': Env.App_Signature,
    };
    final response = await _makeGetRequest(url, headers);
    return response;
  }

  Future<Map<String, dynamic>> deleteAgentIncentiveFile(id) async {
    // print(_token);
    if (_token == null) {
      await _initializeToken();
    }
    // final url = Uri.parse('${Env.apiUrl}agent/agentList');

    final url = Uri.parse('${Env.apiUrl}agent/deleteAgentIncentiveFile?id=$id');
    final headers = {
      'Authorization': 'Bearer $_token' ?? '',
      'app-signature': Env.App_Signature,
    };
    final response = await _makeGetRequest(url, headers);
    return response;
  }

  // ------------------------------------ STAFF -----------------------------------------------------

  Future<Map<String, dynamic>> fetchStaffUserList(int managerId, role) async {
    // print(_token);

    print('rolemanagerId - $managerId');
    if (_token == null) {
      await _initializeToken();
    }

    dynamic url;
    if (role == 'manager') {
      print('manager');
      // url = Uri.parse('${Env.apiUrl}staff/staffList');
      url = Uri.parse('${Env.apiUrl}staff/staffList?manager_id=${managerId}');
    } else if (role == 'Accounts') {
      print('manager');
      // url = Uri.parse('${Env.apiUrl}staff/staffList');
      url = Uri.parse('${Env.apiUrl}staff/staffList?manager_id=${managerId}');
    } else {
      print('handler');
      url = Uri.parse('${Env.apiUrl}staff/staffList?handler_id=${managerId}');
    }

    final headers = {
      'Authorization': 'Bearer $_token' ?? '',
      'app-signature': Env.App_Signature,
    };
    final response = await _makeGetRequest(url, headers);
    return response;
  }

  Future<Map<String, dynamic>> findSingleStaffData(id) async {
    // print(_token);
    if (_token == null) {
      await _initializeToken();
    }
    // final url = Uri.parse('${Env.apiUrl}staff/findStaff?id=$id');


    final url = Uri.parse('${Env.apiUrl}staff/findStaff?id=$id');
    final headers = {
      'Authorization': 'Bearer $_token' ?? '',
      'app-signature': Env.App_Signature,
    };
    final response = await _makeGetRequest(url, headers);
    return response;
  }

  // ------------------------------------ SALES EXECUTIVE -----------------------------------------------------

  Future<Map<String, dynamic>> fetchSalesExecutiveList(
    int managerId,
    role,
  ) async {
    // print(_token);

    print('rolemanagerId - $managerId');
    if (_token == null) {
      await _initializeToken();
    }

    dynamic url;
    if (role == 'manager') {
      print('manager');
      // url = Uri.parse('${Env.apiUrl}staff/staffList');
      url = Uri.parse(
        '${Env.apiUrl}salesExecutive/executiveList?manager_id=${managerId}',
      );
      // url = Uri.parse('${Env.apiUrl}salesExecutive/executiveList');
    } else if (role == 'Accounts') {
      print('manager');
      // url = Uri.parse('${Env.apiUrl}staff/staffList');
      url = Uri.parse(
        '${Env.apiUrl}salesExecutive/executiveList?manager_id=${managerId}',
      );
    } else {
      print('handler');
      url = Uri.parse(
        '${Env.apiUrl}salesExecutive/executiveList?handler_id=${managerId}',
      );
    }

    final headers = {
      'Authorization': 'Bearer $_token' ?? '',
      'app-signature': Env.App_Signature,
    };
    final response = await _makeGetRequest(url, headers);
    return response;
  }

  Future<Map<String, dynamic>> findSingleSalesExecutiveData(id) async {
    // print(_token);
    if (_token == null) {
      await _initializeToken();
    }

    final url = Uri.parse('${Env.apiUrl}salesExecutive/findExecutive?id=$id');
    // final url = Uri.parse('${Env.apiUrl}staff/findStaff?id=$id');
    final headers = {
      'Authorization': 'Bearer $_token' ?? '',
      'app-signature': Env.App_Signature,
    };
    final response = await _makeGetRequest(url, headers);
    return response;
  }

  // ----------------------------------- POS  -------------------------------------------------
  Future<Map<String, dynamic>> fetchPosList(int managerId, val) async {
    // print(_token);

    print('dropDown - $managerId');
    if (_token == null) {
      await _initializeToken();
    }

    dynamic url;
    if (val == 'dropDown') {
      print('dropDown');
      url = Uri.parse(
        '${Env.apiUrl}master/getAllPOS?is_active=1&manager_id=${managerId}',
      );
    } else {
      print('List');
      url = Uri.parse(
        '${Env.apiUrl}master/getAllPOS?is_active=1&manager_id=${managerId}',
      );
    }

    final headers = {
      'Authorization': 'Bearer $_token' ?? '',
      'app-signature': Env.App_Signature,
    };
    final response = await _makeGetRequest(url, headers);
    return response;
  }

  // ----------------------------------- ENQUIRY -------------------------------------------------

  Future<Map<String, dynamic>> fetchEnquiryList(
    int managerid,
    int id,
    String role, {
    String? fromDate,
    String? toDate,
    String? selectedStatus,
    String? selectedStaffId,
  }) async {
    print("selectedStatusselectedStatus - $selectedStatus");
    if (_token == null) {
      await _initializeToken();
    }

    final String query;

    // if (role == 'manager' || role == 'handler') {
    //   query = 'manager_id=$id';
    // }
    if (role == 'manager') {
      query = 'manager_id=$managerid';
    } else if (role == 'handler') {
      query = 'handler_id=$id';
    } else if (role == 'staff') {
      query = 'staff_id=$id';
    } else {
      query = 'agent_id=$id';
    }

    // final url = Uri.parse(
    //   '${Env.apiUrl}enquiry/enquiryList?$query&from_date=${fromDate ?? ''}&to_date=${toDate ?? ''}',
    // );
    final url;

    if (role == 'staff') {
      url = Uri.parse(
        '${Env.apiUrl}enquiry/enquiryList?$query&from_date=${fromDate ?? ''}&to_date=${toDate ?? ''}&enquiry_status=$selectedStatus',
        // '${Env.apiUrl}enquiry/enquiryList?$query&from_date=${fromDate ?? ''}&to_date=${toDate ?? ''}&status=$selectedStatus',
      );
    } else if (role == 'agent') {
      url = Uri.parse(
        '${Env.apiUrl}enquiry/enquiryList?$query&from_date=${fromDate ?? ''}&to_date=${toDate ?? ''}&status=$selectedStatus',
      );
    } else {
      url = Uri.parse(
        '${Env.apiUrl}enquiry/enquiryList?$query&from_date=${fromDate ?? ''}&to_date=${toDate ?? ''}&enquiry_status=$selectedStatus&staff_id=$selectedStaffId',
        // '${Env.apiUrl}enquiry/enquiryList?$query&from_date=${fromDate ?? ''}&to_date=${toDate ?? ''}&status=$selectedStatus&staff_id=$selectedStaffId',
      );
    }

    final headers = {
      'Authorization': 'Bearer $_token' ?? '',
      'app-signature': Env.App_Signature,
    };

    final response = await _makeGetRequest(url, headers);
    return response;
  }

  Future<Map<String, dynamic>> fetchAttndanceOFAllStaffList({
    required String month,
    required dynamic mangerId,
  }) async {
    if (_token == null) {
      await _initializeToken();
    }

    final String query;

    // if (role == 'manager') {
    //   query = 'manager_id=$id';
    // }

    final url = Uri.parse(
      '${Env.apiUrl}staff/monthlyLoginCount?month=${month ?? ''}&manager_id=$mangerId',
    );

    final headers = {
      'Authorization': 'Bearer $_token' ?? '',
      'app-signature': Env.App_Signature,
    };

    final response = await _makeGetRequest(url, headers);
    return response;
  }

  Future<Map<String, dynamic>> fetchAttndanceOfIndiviualStaffList({
    String? month,
    id,
  }) async {
    if (_token == null) {
      await _initializeToken();
    }

    final String query;

    final url = Uri.parse(
      '${Env.apiUrl}staff/staffLoggedDays?month=${month ?? ''}&staff_id=$id',
    );

    final headers = {
      'Authorization': 'Bearer $_token' ?? '',
      'app-signature': Env.App_Signature,
    };

    final response = await _makeGetRequest(url, headers);
    return response;
  }

  Future<Map<String, dynamic>> findSingleEnquiryData(id) async {
    // print(_token);
    if (_token == null) {
      await _initializeToken();
    }
    final url = Uri.parse('${Env.apiUrl}enquiry/enquiryList?enquiry_id=$id');
    final headers = {
      'Authorization': 'Bearer $_token' ?? '',
      'app-signature': Env.App_Signature,
    };
    final response = await _makeGetRequest(url, headers);
    return response;
  }

  Future<Map<String, dynamic>> updateEnquiryInProgress(id) async {
    // print(_token);
    if (_token == null) {
      await _initializeToken();
    }
    final url = Uri.parse(
      '${Env.apiUrl}enquiry/updateEnquiryInProgress?enquiry_id=$id',
    );
    final headers = {
      'Authorization': 'Bearer $_token' ?? '',
      'app-signature': Env.App_Signature,
    };
    final response = await _makeGetRequest(url, headers);
    return response;
  }

  Future<Map<String, dynamic>> findEnqQuotePolicyView(id) async {
    print('findEnqQuotePolicyVie2w - $id');
    if (_token == null) {
      print('findEnqQuotePolicyVie2w 1');
      await _initializeToken();
      print('findEnqQuotePolicyVie2w 2');
    }
    final url = Uri.parse(
      '${Env.apiUrl}enquiry/enquiryQuotePolicyView?enquiry_id=$id',
    );
    final headers = {
      'Authorization': 'Bearer $_token' ?? '',
      'app-signature': Env.App_Signature,
    };
    print('findEnqQuotePolicyVie2w 3');
    final response = await _makeGetRequest(url, headers);

    if (response.containsKey('error')) {
      print(
        '❌ API returned invalid JSON or HTML. Raw response: ${response['raw']}',
      );
      return {}; // return empty map on error
    }
    print('findEnqQuotePolicyView 1');
    print('findEnqQuotePolicyView 1 - $response');
    return response;
  }

  Future<Map<String, dynamic>> deleteEnquiy(id, val) async {
    final url = Uri.parse(
      '${Env.apiUrl}/enquiry/updateEnquiryStatus?enquiry_id=$id&is_active=$val',
    );

    // final token = await getToken(); // Fetch token

    if (_token == null) {
      throw Exception('Token not found. Please log in.');
    }

    final headers = {
      'Authorization': 'Bearer $_token',
      'Content-Type': 'application/json',
      'app-signature': Env.App_Signature,
      // 'app-signature': 'nhance-partner-2025-signature-35468846JRhH551HK',
    };

    // final response = await _makePostRequestJson(url, data, headers);
    final response = await _makeGetRequest(url, headers);
    return response;
  }

  //------------------------------------ Quotation ------------------------------------------------

  Future<Map<String, dynamic>> fetchQuickQuotationList(int managerId) async {
    // print(_token);
    if (_token == null) {
      await _initializeToken();
    }

    final url = Uri.parse(
      '${Env.apiUrl}enquiry/getQuickQuoteEnquiryList?manager_id=${managerId}',
    );
    final headers = {
      'Authorization': 'Bearer $_token' ?? '',
      'app-signature': Env.App_Signature,
    };
    final response = await _makeGetRequest(url, headers);
    return response;
  }

  Future<Map<String, dynamic>> fetchQuotationList(int managerId) async {
    // print(_token);
    if (_token == null) {
      await _initializeToken();
    }

    final url = Uri.parse(
      '${Env.apiUrl}quotation/quotationList?manager_id=${managerId}',
    );
    final headers = {
      'Authorization': 'Bearer $_token' ?? '',
      'app-signature': Env.App_Signature,
    };
    final response = await _makeGetRequest(url, headers);
    return response;
  }

  Future<Map<String, dynamic>> findSingleQuotationData(id) async {
    // print(_token);
    if (_token == null) {
      await _initializeToken();
    }
    final url = Uri.parse('${Env.apiUrl}quotation/findQuotation?id=$id');
    final headers = {
      'Authorization': 'Bearer $_token' ?? '',
      'app-signature': Env.App_Signature,
    };
    final response = await _makeGetRequest(url, headers);
    return response;
  }

  // ----------------------------------- POLICY  -------------------------------------------------

  Future<Map<String, dynamic>> fetchPolicyList(
    int id,
    role, {
    String? fromDate,
    String? toDate,
    String? selectedStatus,
    String? selectedStaffId,
  }) async {
    print("APISERV : A1304");
    print(_token);
    if (_token == null) {
      await _initializeToken();
    }

    final url;

    final String query;
    final String val = 'Proposal Created';
    if (role == 'manager') {
      query = 'manager_id=$id';
    } else if (role == 'staff') {
      query = 'staff_id=$id';
    } else {
      query = 'agent_id=$id';
    }

    // url = Uri.parse(
    //   '${Env.apiUrl}enquiry/enquiryList?$query&from_date=${fromDate ?? ''}&to_date=${toDate ?? ''}',
    // );

    if (role == 'staff') {
      url = Uri.parse(
        '${Env.apiUrl}enquiry/enquiryList?$query&from_date=${fromDate ?? ''}&to_date=${toDate ?? ''}&status=$selectedStatus',
      );
    } else {
      url = Uri.parse(
        '${Env.apiUrl}enquiry/enquiryList?$query&from_date=${fromDate ?? ''}&to_date=${toDate ?? ''}&status=$selectedStatus&staff_id=$selectedStaffId',
      );
    }

    // if (role == 'manager') {
    //   url = Uri.parse('${Env.apiUrl}enquiry/enquiryList?manager_id=$id');
    // } else {
    //   url = Uri.parse('${Env.apiUrl}enquiry/enquiryList?staff_id=$id');
    // }
    // else {
    //   url = Uri.parse('${Env.apiUrl}endorsement/endorsementList?agent_id=$id');
    // }

    final headers = {
      'Authorization': 'Bearer $_token' ?? '',
      'app-signature': Env.App_Signature,
    };
    final response = await _makeGetRequest(url, headers);
    return response;
  }

  Future<Map<String, dynamic>> fetchPolicyDataOnlyList(
    managerId,
    dynamic id,
    role, {
    String? fromDate,
    String? toDate,
    String? selectedStaffId,
        String ? selectedStatus,
        String ? selectedInsurer,
  }) async {
    print(
      "fetchPolicyDataOnlyListAPI - mangerID- $managerId - id - $id - role -$role >> selectedInsurer -$selectedInsurer ",
    );
    if (_token == null) {
      await _initializeToken();
    }

    final userId = '1';

    String query = "";

    if (role == 'manager') {
      query = 'manager_id=$managerId';
    } else if (role == 'Accounts') {
      query = 'manager_id=$managerId';
      if (selectedStatus != null) { query += '&show_policy_report=${Uri.encodeComponent(selectedStatus)}'; }
      if (selectedInsurer != null && selectedInsurer.toString().trim().isNotEmpty) { query += '&insurer_id=$selectedInsurer'; }
    } else if (role == 'staff') {
      query = 'staff_id=$id';
    } else if (role == 'handler') {
      query = 'handler_id=$id';
    } else {
      query = 'agent_id=$id';
    }

    final url = Uri.parse(
      '${Env.apiUrl}enquiry/enquiryList?$query&only_policy_data=true&from_date=${fromDate ?? ''}&to_date=${toDate ?? ''}&staff_id=$selectedStaffId',
    );
    // final url = Uri.parse(
    //   'http://localhost/nhance_partner_be/enquiry/enquiryList?$query&only_policy_data=true&from_date=${fromDate ?? ''}&to_date=${toDate ?? ''}&staff_id=$selectedStaffId',
    // );

    // if (role == 'manager') {
    //   url = Uri.parse(
    //     '${Env.apiUrl}enquiry/enquiryList?manager_id=$id&only_policy_data=true',
    //   );
    // } else if (role == 'staff') {
    //   url = Uri.parse(
    //     '${Env.apiUrl}enquiry/enquiryList?staff_id=$id&only_policy_data=true',
    //   );
    // } else {
    //   url = Uri.parse(
    //     '${Env.apiUrl}enquiry/enquiryList?agent_id=$id&only_policy_data=true',
    //   );
    // }


    final headers = {
      'Authorization': 'Bearer $_token' ?? '',
      'app-signature': Env.App_Signature,
    };
    final response = await _makeGetRequest(url, headers);
    return response;
  }

  // ----------------------------------- CLAIMS -------------------------------------------------

  Future<Map<String, dynamic>> fetchPolicySearch(val) async {
    // print(_token);

    // print('rolemanagerId - $managerId');
    if (_token == null) {
      await _initializeToken();
    }

    dynamic url;
    url = Uri.parse('${Env.apiUrl}policy/searchThePolicies?policy_number=$val');

    final headers = {
      'Authorization': 'Bearer $_token' ?? '',
      'app-signature': Env.App_Signature,
    };
    final response = await _makeGetRequest(url, headers);
    return response;
  }

  Future<Map<String, dynamic>> fetchVehicleSearch(val) async {
    // print(_token);

    // print('rolemanagerId - $managerId');
    if (_token == null) {
      await _initializeToken();
    }

    dynamic url;

    url = Uri.parse(
      '${Env.apiUrl}policy/searchThePolicies?vehicle_number=$val',
    );

    final headers = {
      'Authorization': 'Bearer $_token' ?? '',
      'app-signature': Env.App_Signature,
    };
    final response = await _makeGetRequest(url, headers);
    return response;
  }

  Future<Map<String, dynamic>> fetchClaimList(
    managerId,
    int userId,
    role,
  ) async {
    // print(_token);
    if (_token == null) {
      await _initializeToken();
    }

    final url;

    if (role == 'manager') {
      url = Uri.parse('${Env.apiUrl}claim/ClaimList?manager_id=$managerId');
    } else if (role == 'Accounts') {
      url = Uri.parse('${Env.apiUrl}claim/ClaimList?manager_id=$managerId');
    } else if (role == 'agent') {
      url = Uri.parse('${Env.apiUrl}claim/ClaimList?agent_id=$userId');
    } else if (role == 'handler') {
      url = Uri.parse('${Env.apiUrl}claim/ClaimList?handler_id=$userId');
    } else {
      url = Uri.parse('${Env.apiUrl}claim/ClaimList?staff_id=$userId');
    }


    final headers = {
      'Authorization': 'Bearer $_token' ?? '',
      'app-signature': Env.App_Signature,
    };
    final response = await _makeGetRequest(url, headers);
    return response;
  }

  Future<Map<String, dynamic>> findSingleClaimData(id) async {
    // print(_token);
    if (_token == null) {
      await _initializeToken();
    }
    final url = Uri.parse('${Env.apiUrl}claim/ClaimList?policy_number=$id');

    final headers = {
      'Authorization': 'Bearer $_token' ?? '',
      'app-signature': Env.App_Signature,
    };
    final response = await _makeGetRequest(url, headers);
    return response;
  }

  // ----------------------------------- ENDORSEMENT -------------------------------------------------

  Future<Map<String, dynamic>> fetchEndorsementList(
      managerId,
      int id,
      role, {
        String? fromDate,
        String? toDate,
        String? endorsementType,
        String? insurerId,
        String? status,
        String? verification,
      }) async {
    print('fetchEndorsementList in');
    if (_token == null) {
      await _initializeToken();
    }
    print('fetchEndorsementList token');

    Map<String, String> queryParams = {};

    // 🔹 Role based base params
    if (role == 'manager' || role == 'Accounts') {
      queryParams['manager_id'] = managerId.toString();
    } else if (role == 'agent') {
      queryParams['agent_id'] = id.toString();
    } else if (role == 'handler') {
      queryParams['handler_id'] = id.toString();
    } else {
      queryParams['staff_id'] = id.toString();
    }

    queryParams['from_date'] = fromDate ?? '';
    queryParams['to_date'] = toDate ?? '';
    queryParams['endorsement_type'] = endorsementType ?? '';
    queryParams['insurer_id'] = insurerId ?? '';
    queryParams['status'] = status ?? '';
    queryParams['verification'] = verification ?? '';

    print('fetchEndorsementList api');

    final url = Uri.parse('${Env.apiUrl}endorsement/endorsementList')
        .replace(queryParameters: queryParams);

    final headers = {
      'Authorization': 'Bearer $_token',
      'app-signature': Env.App_Signature,
    };

    final response = await _makeGetRequest(url, headers);
    return response;
  }

  Future<Map<String, dynamic>> findSingleEnrosmentData(id) async {
    // print(_token);
    if (_token == null) {
      await _initializeToken();
    }
    final url = Uri.parse(
      '${Env.apiUrl}endorsement/endorsementList?policy_number=$id',
    );

    final headers = {
      'Authorization': 'Bearer $_token' ?? '',
      'app-signature': Env.App_Signature,
    };
    final response = await _makeGetRequest(url, headers);
    return response;
  }

  // ---------------------------------   MASTER DATA  DROPDOWN----------------------------------------------

  Future<Map<String, dynamic>> fetchMasterDropDown(String val ,[String? type]) async {
    // print(_token);
    print("*** MasterDropDown *** ");
    if (_token == null) {
      await _initializeToken();
    }

    print('VAl - $val');

    dynamic url;

    if (val == 'staffRole') {
      url = Uri.parse('${Env.apiUrl}master/getRoleMaster');
    } else if (val == 'vehicleType') {
      // url = Uri.parse('${Env.apiUrl}master/getVehicleTypeMaster');
      if(type == 'dropdown'){
        url = Uri.parse('${Env.apiUrl}master/getVehicleTypeMaster?value_for=dropdown');
      }else{
        url = Uri.parse('${Env.apiUrl}master/getVehicleTypeMaster');
      }
      // url = Uri.parse('${Env.apiUrl}master/getVehicleTypeMaster')
      //     .replace(queryParameters: {
      //   if (type != null) 'value_for': type,
      //});
    } else if (val == 'InsuranceType') {
      url = Uri.parse('${Env.apiUrl}master/getInsurancePlanTypeMaster');
    } else if (val == 'Insurers') {
      url = Uri.parse('${Env.apiUrl}master/getInsurersMaster');
    } else if (val == 'Staffs') {
      url = Uri.parse('${Env.apiUrl}master/getStaffMaster');
    } else if (val == 'Claim') {
      url = Uri.parse('${Env.apiUrl}master/getClaimMaster');
    } else if (val == 'Endorsement') {
      url = Uri.parse('${Env.apiUrl}master/getEndorsementMaster');
    } else if (val == 'Broker') {
      url = Uri.parse('${Env.apiUrl}master/getAllBrokers');
    } else if (val == 'PaymentMode') {
      url = Uri.parse('${Env.apiUrl}master/getAllPaymentModelist');
    } else if (val == 'EndorsementType') {
      url = Uri.parse('${Env.apiUrl}master/getAllEndorsement');
    }
    final headers = {
      'Authorization': 'Bearer $_token' ?? '',
      'app-signature': Env.App_Signature,
    };
    final response = await _makeGetRequest(url, headers);
    return response;
  }

  Future<Map<String, dynamic>> fetchHandlerNameDropDown(id) async {
    print('fetchHandlerNameDropDown');
    if (_token == null) {
      await _initializeToken();
    }

    dynamic url;
    print('fetchHandlerNameDropDown 1');
    url = Uri.parse(
      '${Env.apiUrl}staff/handlerListForStaffCreationDropdown?manager_id=$id',
    );
    print('fetchHandlerNameDropDown 2');
    final headers = {
      'Authorization': 'Bearer $_token' ?? '',
      'app-signature': Env.App_Signature,
    };
    print('fetchHandlerNameDropDown 3');
    final response = await _makeGetRequest(url, headers);
    print('fetchHandlerNameDropDown 4 - $response');
    return response;
  }

  Future<Map<String, dynamic>> fetchStaffListForEnquiryAssignDropDown(
    managerId,
    id,
    role,
  ) async {
    print('fetchHandlerNameDropDown');
    if (_token == null) {
      await _initializeToken();
    }

    dynamic url;
    print('fetchHandlerNameDropDown 1');

    if (role == 'manager') {
      url = Uri.parse(
        '${Env.apiUrl}staff/staffListForEnquiryAssignDropdown?manager_id=$managerId',
      );
    } else {
      url = Uri.parse(
        '${Env.apiUrl}staff/staffListForEnquiryAssignDropdown?handler_id=$id',
      );
    }
    print('fetchHandlerNameDropDown 2');
    final headers = {
      'Authorization': 'Bearer $_token' ?? '',
      'app-signature': Env.App_Signature,
    };
    print('fetchHandlerNameDropDown 3');
    final response = await _makeGetRequest(url, headers);
    print('fetchHandlerNameDropDown 4 - $response');
    return response;
  }

  Future<Map<String, dynamic>> fetchAgentNameDropDown(id) async {
    print('fetchAGENTNameDropDown');
    if (_token == null) {
      await _initializeToken();
    }

    dynamic url;
    print('fetchAGENTNameDropDown 1');
    url = Uri.parse(
      '${Env.apiUrl}agent/agentListForEnquiryCreationDropdown?manager_id=$id',
    );
    print('fetchAGENTNameDropDown 2');
    final headers = {
      'Authorization': 'Bearer $_token' ?? '',
      'app-signature': Env.App_Signature,
    };
    print('fetchAGENTNameDropDown 3');
    final response = await _makeGetRequest(url, headers);
    print('fetchAGENTNameDropDown 4 - $response');
    return response;
  }

  Future<Map<String, dynamic>> fetchAgentUnusedCommissionList(id,broker_id) async {
    print('fetchAGENTNameDropDown');
    if (_token == null) {
      await _initializeToken();
    }

    dynamic url;
    print('fetchAGENTNameDropDown 1');
    url = Uri.parse(
        '${Env.apiUrl}invoice/getAgentUnusedCommissionList?manager_id=$id&broker_id=$broker_id',
    );
    print('fetchAGENTNameDropDown 2');
    final headers = {
      'Authorization': 'Bearer $_token' ?? '',
      'app-signature': Env.App_Signature,
    };
    print('fetchAGENTNameDropDown 3');
    final response = await _makeGetRequest(url, headers);
    print('fetchAGENTNameDropDown 4 - $response');
    return response;
  }

  static late String _baseUrl;

  static void initialize(String baseUrl) {
    _baseUrl = Env.apiUrl;
  }

  // Fetch all enquiries
  static Future<List<EnquiryModel>> fetchEnquiries() async {
    try {
      final response = await http.get(
        Uri.parse('$_baseUrl/api/enquiries'),
        headers: {'Content-Type': 'application/json'},
      );

      if (response.statusCode == 200) {
        final List<dynamic> data = json.decode(response.body);
        return data.map((json) => EnquiryModel.fromJson(json)).toList();
      } else {
        throw Exception('Failed to load enquiries');
      }
    } catch (e) {
      throw Exception('Error fetching enquiries: $e');
    }
  }

  // Create new enquiry
  static Future<EnquiryModel> createEnquiry(EnquiryModel enquiry) async {
    try {
      final response = await http.post(
        Uri.parse('$_baseUrl/api/enquiries'),
        headers: {'Content-Type': 'application/json'},
        body: json.encode(enquiry.toJson()),
      );

      if (response.statusCode == 200 || response.statusCode == 201) {
        return EnquiryModel.fromJson(json.decode(response.body));
      } else {
        throw Exception('Failed to create enquiry');
      }
    } catch (e) {
      throw Exception('Error creating enquiry: $e');
    }
  }

  // Update enquiry
  static Future<EnquiryModel> updateEnquiry(
    String id,
    EnquiryModel enquiry,
  ) async {
    try {
      final response = await http.put(
        Uri.parse('$_baseUrl/api/enquiries/$id'),
        headers: {'Content-Type': 'application/json'},
        body: json.encode(enquiry.toJson()),
      );

      if (response.statusCode == 200) {
        return EnquiryModel.fromJson(json.decode(response.body));
      } else {
        throw Exception('Failed to update enquiry');
      }
    } catch (e) {
      throw Exception('Error updating enquiry: $e');
    }
  }

  // Fetch dropdown options
  static Future<List<DropdownOption>> fetchDropdownOptions(String type) async {
    try {
      final response = await http.get(
        Uri.parse('$_baseUrl/api/options/$type'),
        headers: {'Content-Type': 'application/json'},
      );

      if (response.statusCode == 200) {
        final List<dynamic> data = json.decode(response.body);
        return data.map((json) => DropdownOption.fromJson(json)).toList();
      } else {
        throw Exception('Failed to load options');
      }
    } catch (e) {
      throw Exception('Error fetching options: $e');
    }
  }

  // ---------------------------------   PayOut  Module----------------------------------------------
  Future<Map<String, dynamic>> getCommissionRateList(data) async {
    print("getCommissionRateList-------  $data}");
    final url = Uri.parse('${Env.apiUrl}/invoice/commission-rate-list');
    print("getCommissionRateList 1");
    // final token = await getToken(); // Fetch token

    // if (_token == null) {
    //   throw Exception('Token not found. Please log in.');
    // }

    if (_token == null) {
      await _initializeToken();
    }
    print("getCommissionRateList 2");
    print("data-------  $data}");

    final headers = {
      'Authorization': 'Bearer $_token',
      'Content-Type': 'application/json',
      'app-signature': Env.App_Signature,
      // 'app-signature': 'nhance-partner-2025-signature-35468846JRhH551HK',
    };

    final response = await _makePostRequestJson(url, data, headers);
    return response;
  }

  Future<Map<String, dynamic>> getCreateOrUpdate(data) async {
    final url = Uri.parse('${Env.apiUrl}invoice/create-or-update');

    // final token = await getToken(); // Fetch token

    if (_token == null) {
      throw Exception('Token not found. Please log in.');
    }

    print("data-------  $data}");

    final headers = {
      'Authorization': 'Bearer $_token',
      'Content-Type': 'application/json',
      'app-signature': Env.App_Signature,
      // 'app-signature': 'nhance-partner-2025-signature-35468846JRhH551HK',
    };

    final response = await _makePostRequestJson(url, data, headers);
    return response;
  }

  Future<Map<String, dynamic>> getInvoiceList() async {
    // print(_token);
    if (_token == null) {
      await _initializeToken();
    }

    final url = Uri.parse('${Env.apiUrl}invoice/list');

    final headers = {
      'Authorization': 'Bearer $_token' ?? '',
      'app-signature': Env.App_Signature,
    };
    final response = await _makeGetRequest(url, headers);
    return response;
  }

  Future<Map<String, dynamic>> deleteInvoice(ID) async {
    // print(_token);
    if (_token == null) {
      await _initializeToken();
    }

    final url = Uri.parse('${Env.apiUrl}invoice/delete?id=$ID');

    final headers = {
      'Authorization': 'Bearer $_token' ?? '',
      'app-signature': Env.App_Signature,
    };
    final response = await _makeGetRequest(url, headers);
    return response;
  }

  Future<Map<String, dynamic>> delete(ID, path) async {
    // print(_token);
    if (_token == null) {
      await _initializeToken();
    }

    final url = Uri.parse('${Env.apiUrl}$path');

    final headers = {
      'Authorization': 'Bearer $_token' ?? '',
      'app-signature': Env.App_Signature,
    };
    final response = await _makeGetRequest(url, headers);
    return response;
  }

  Future<Map<String, dynamic>> getInvoiceDetails(ID) async {
    // print(_token);
    if (_token == null) {
      await _initializeToken();
    }

    final url = Uri.parse('${Env.apiUrl}invoice/details?id=$ID');

    final headers = {
      'Authorization': 'Bearer $_token' ?? '',
      'app-signature': Env.App_Signature,
    };
    final response = await _makeGetRequest(url, headers);
    return response;
  }

  Future<Map<String, dynamic>> findPolicyApi(ID) async {
    // print(_token);
    if (_token == null) {
      await _initializeToken();
    }

    final url = Uri.parse('${Env.apiUrl}policy/findPolicy?id=$ID');

    final headers = {
      'Authorization': 'Bearer $_token' ?? '',
      'app-signature': Env.App_Signature,
    };
    final response = await _makeGetRequest(url, headers);
    return response;
  }

  Future<Map<String, dynamic>> policyFilePathApi(ID, file_type) async {
    // print(_token);
    if (_token == null) {
      await _initializeToken();
    }

    final url = Uri.parse(
      '${Env.apiUrl}policy/PolicyFilePath?policy_id=$ID&file_type=$file_type',
    );

    final headers = {
      'Authorization': 'Bearer $_token' ?? '',
      'app-signature': Env.App_Signature,
    };
    final response = await _makeGetRequest(url, headers);
    return response;
  }

  Future<Map<String, dynamic>> updatePolicyApi(data) async {
    final url = Uri.parse('${Env.apiUrl}policy/updatePolicy');

    // final token = await getToken(); // Fetch token

    if (_token == null) {
      throw Exception('Token not found. Please log in.');
    }

    print("data-------  $data}");

    final headers = {
      'Authorization': 'Bearer $_token',
      'Content-Type': 'application/json',
      'app-signature': Env.App_Signature,
      // 'app-signature': 'nhance-partner-2025-signature-35468846JRhH551HK',
    };

    final response = await _makePostRequestJson(url, data, headers);
    return response;
  }

  Future<Map<String, dynamic>> calculateCommissionRequest(data) async {
    final url = Uri.parse('${Env.apiUrl}policy/calculateCommissionRequest');

    // final token = await getToken(); // Fetch token

    if (_token == null) {
      throw Exception('Token not found. Please log in.');
    }

    print("data-------  $data}");

    final headers = {
      'Authorization': 'Bearer $_token',
      'Content-Type': 'application/json',
      'app-signature': Env.App_Signature,
      // 'app-signature': 'nhance-partner-2025-signature-35468846JRhH551HK',
    };

    final response = await _makePostRequestJson(url, data, headers);
    return response;
  }

  Future<Map<String, dynamic>> fetchPolicyMasterDropDown(String val) async {
    // print(_token);
    if (_token == null) {
      await _initializeToken();
    }

    print('VAl - $val');

    dynamic url;

    if (val == 'fuelType') {
      url = Uri.parse('${Env.apiUrl}policy/fuelTypeMaster');
    } else if (val == 'vehicleType') {
      url = Uri.parse('${Env.apiUrl}policy/vehicleTypeMaster');
    }

    final headers = {
      'Authorization': 'Bearer $_token' ?? '',
      'app-signature': Env.App_Signature,
    };
    final response = await _makeGetRequest(url, headers);
    return response;
  }


  Future<void> generateEndorsementExcel(managerId,searchValue) async {
    final url = Uri.parse(
      '${Env.apiUrl}reports/endorsement-excel?manager_id=$managerId',
    );


    print('getPdfDownload endorsement-excel - $url');
    await _initializeToken();

    if (_token == null) throw Exception('Token not found. Please log in.');

    final headers = {
      'Authorization': 'Bearer $_token',
      // 'App-Signature': Env.App_Signature,
      'app-signature': Env.App_Signature,
    };

    final response = await _makeGethttpRequest(url, headers);

    if (response.statusCode == 200) {
      final contentType = response.headers['content-type'] ?? '';
      if (contentType.contains('application/json')) {
        final jsonResponse = jsonDecode(response.body);
        if (jsonResponse['code'] == 404 || jsonResponse['code'] == 500) {
          ToastHelper.show('File Not Found', Colors.red);
        } else {
          ToastHelper.show('No File Found', Colors.red);
        }
        return;
      } else {
        final blob = html.Blob([response.bodyBytes]);
        final blobUrl = html.Url.createObjectUrlFromBlob(blob);
        print('getPdfDownload $blobUrl');
        final contentDisp = response.headers['content-disposition'];
        print('getPdfDownloadcontentDisp  $contentDisp');

        String fileName = extractFileName(contentDisp, "policy_excel");
        print('Resolved fileName: $fileName');
        final anchor = html.AnchorElement(href: blobUrl)
          ..setAttribute('download', fileName)
          ..click();

        html.Url.revokeObjectUrl(blobUrl);
      }
    } else if (response.statusCode == 404) {
      showDialog(
        context: context,
        builder: (context) => AlertDialog(
          title: const Text('File not found.'),
          actions: [
            TextButton(
              child: const Text('OK'),
              onPressed: () => Navigator.of(context).pop(),
            ),
          ],
        ),
      );
    } else if (response.statusCode == 403) {
      await clearLocalStorageAndRedirect();
    } else if (response.statusCode == 500) {
      ToastHelper.show('No File Found', Colors.red);
    } else if (response.statusCode == 302) {
      print('getPdfDownload 3-PP-302');
      ToastHelper.show('No File Found', Colors.red);
    } else {
      ToastHelper.show('No File Found', Colors.red);
      throw Exception('Failed to download file');
    }
  }

  Future<void> generatePolicyExcel(managerId, fromDate, toDate, searchValue,flag,insurer) async {
    final url = Uri.parse(
      '${Env.apiUrl}reports/policy-excel?manager_id=$managerId&from_date=${fromDate ?? ''}&to_date=${toDate ?? ''}&search=${searchValue ?? ''}&show_policy_report=${flag ?? ''}&insurer_id=${insurer ?? ''}',
    );
    print('getPdfDownload policy-excel - $url');
    await _initializeToken();

    if (_token == null) throw Exception('Token not found. Please log in.');

    final headers = {
      'Authorization': 'Bearer $_token',
      // 'App-Signature': Env.App_Signature,
      'app-signature': Env.App_Signature,
    };

    final response = await _makeGethttpRequest(url, headers);

    if (response.statusCode == 200) {
      final contentType = response.headers['content-type'] ?? '';
      if (contentType.contains('application/json')) {
        final jsonResponse = jsonDecode(response.body);
        if (jsonResponse['code'] == 404 || jsonResponse['code'] == 500) {
          ToastHelper.show('File Not Found', Colors.red);
        } else {
          ToastHelper.show('No File Found', Colors.red);
        }
        return;
      } else {
        final blob = html.Blob([response.bodyBytes]);
        final blobUrl = html.Url.createObjectUrlFromBlob(blob);
        print('getPdfDownload $blobUrl');
        final contentDisp = response.headers['content-disposition'];
        print('getPdfDownloadcontentDisp  $contentDisp');

        String fileName = extractFileName(contentDisp, "policy_excel");
        print('Resolved fileName: $fileName');
        final anchor = html.AnchorElement(href: blobUrl)
          ..setAttribute('download', fileName)
          ..click();

        html.Url.revokeObjectUrl(blobUrl);
      }
    } else if (response.statusCode == 404) {
      showDialog(
        context: context,
        builder: (context) => AlertDialog(
          title: const Text('File not found.'),
          actions: [
            TextButton(
              child: const Text('OK'),
              onPressed: () => Navigator.of(context).pop(),
            ),
          ],
        ),
      );
    } else if (response.statusCode == 403) {
      await clearLocalStorageAndRedirect();
    } else if (response.statusCode == 500) {
      ToastHelper.show('No File Found', Colors.red);
    } else if (response.statusCode == 302) {
      print('getPdfDownload 3-PP-302');
      ToastHelper.show('No File Found', Colors.red);
    } else {
      ToastHelper.show('No File Found', Colors.red);
      throw Exception('Failed to download file');
    }
  }

  Future<Map<String, dynamic>> fetchAuditHistory(id,table) async {
    print("fetchAuditHistory - $id ");

    if (_token == null) {
      await _initializeToken();
    }
    // final url = Uri.parse('${Env.apiUrl}audit/history?table_name=partner_policy&pk=80',);
    // final url = Uri.parse(
    //   '${Env.apiUrl}audit/history?table_name=partner_policy&pk=${id ?? ''}',
    // );
    final url = Uri.parse(
      '${Env.apiUrl}audit/history?table_name=${table ?? ''}&pk=${id ?? ''}',
    );

    final headers = {
      'Authorization': 'Bearer $_token' ?? '',
      'app-signature': Env.App_Signature,
    };

    final response = await _makeGetRequest(url, headers);

    return response;
  }

  // Future<Map<String, dynamic>> fetchPolicyExcel(managerId,fromDate,toDate) async {
  //
  //   print( "fetchPolicyExcel - mangerID- $managerId - Dated - $fromDate - $toDate " );
  //
  //   if (_token == null) {
  //     await _initializeToken();
  //   }
  //
  //   final String query;
  //
  //   query = 'manager_id=$managerId';
  //
  //   final url = Uri.parse('${Env.apiUrl}reports/policy-excel?$query&from_date=${fromDate ?? ''}&to_date=${toDate ?? ''}',);
  //
  //   final headers = {
  //     'Authorization': 'Bearer $_token' ?? '',
  //     'app-signature': Env.App_Signature,
  //   };
  //
  //   final response = await _makeGetRequest(url, headers);
  //
  //   return response;
  // }

  Future<Map<String, dynamic>> deleteEndorsement(id) async {

    if (_token == null) {
      await _initializeToken();
    }
    final url = Uri.parse('${Env.apiUrl}endorsement/deleteEndorsement?id=$id');
    // final url = Uri.parse('http://localhost/nhance_partner_be/endorsement/deleteEndorsement?id=$id');

    final headers = {
      'Authorization': 'Bearer $_token' ?? '',
      'app-signature': Env.App_Signature,
    };
    final response = await _makeGetRequest(url, headers);
    return response;
  }


  Future<Map<String, dynamic>> uploadEndorsementFile({
    required PlatformFile file,
    required Map<String, dynamic> data,
  }) async {
    if (_token == null) {
      await _initializeToken();
    }

    final url = Uri.parse('${Env.apiUrl}endorsement/uploadEndorsementFile');
    // final url = Uri.parse('http://localhost/nhance_partner_be/endorsement/uploadEndorsementFile');

    try {
      final request = http.MultipartRequest('POST', url);

      request.headers.addAll({
        'Authorization': 'Bearer $_token',
        'app-signature': Env.App_Signature,
      });

      if (file.bytes == null) throw Exception('Could not read file bytes');

      request.files.add(
        http.MultipartFile.fromBytes(
          'endorsement_completion_file',
          file.bytes!,
          filename: file.name,
        ),
      );

      data.forEach((key, value) {
        if (value != null) {
          request.fields[key] = value.toString();
        }
      });

      print('uploadEndorsementFile url => $url');
      print('uploadEndorsementFile fields => ${request.fields}');
      print('uploadEndorsementFile fileName => ${file.name}');

      final streamedResponse = await request.send();
      final responseBody = await streamedResponse.stream.bytesToString();

      print('uploadEndorsementFile raw response => $responseBody');

      if (streamedResponse.statusCode == 401 ||
          streamedResponse.statusCode == 403) {
        await clearLocalStorageAndRedirect();
        return {'status': 'error', 'message': 'Session expired'};
      }

      if (streamedResponse.statusCode != 200) {
        return {
          'status': 'error',
          'message': 'Server Error: ${streamedResponse.statusCode}',
        };
      }

      try {
        final decoded = jsonDecode(responseBody);
        return decoded is Map<String, dynamic>
            ? decoded
            : {'status': 'error', 'message': 'Unexpected response format'};
      } catch (_) {
        return {'status': 'error', 'message': 'Failed to parse response'};
      }
    } catch (e) {
      print('uploadEndorsementFile exception => $e');
      return {'status': 'error', 'message': e.toString()};
    }
  }
}
