import 'package:get/get.dart';
import 'package:shared_preferences/shared_preferences.dart';

class DeliveryController extends GetxController {
  // Exposed reactive fields if needed in UI
  final RxString orderId = ''.obs;
  final RxString orderStatus = ''.obs;
  final RxInt tenantId = 0.obs;
  final RxInt partnerId = 0.obs;
  final RxInt locationId = 0.obs;
  final RxInt orderHeaderId = 0.obs;
  final RxInt deliveryId = 0.obs;
  final RxInt userId = 0.obs;
  final RxString latitude = ''.obs;
  final RxString longitude = ''.obs;

  // Preference keys (aligned with existing usage in app)
  static const String kTenantId = 'delivery_tenantid';
  static const String kPartnerId = 'delivery_partnerid';
  static const String kLocationId = 'delivery_locationid';
  static const String kOrderHeaderId = 'delivery_orderheaderid';
  static const String kDeliveryId = 'delivery_deliveryid';
  static const String kUserId = 'delivery_userid';
  static const String kOrderId = 'delivery_orderid';
  static const String kOrderStatus = 'delivery_orderstatus';
  static const String kLat = 'delivery_latitude';
  static const String kLng = 'delivery_longitude';

  Future<void> saveFromQueueItem(Map<String, dynamic> delivery) async {
    final prefs = await SharedPreferences.getInstance();

    final int? tenantid = delivery['tenantid'] is int
        ? delivery['tenantid'] as int
        : int.tryParse('${delivery['tenantid'] ?? ''}');
    final int? partnerid = delivery['partnerid'] is int
        ? delivery['partnerid'] as int
        : int.tryParse('${delivery['partnerid'] ?? ''}');
    final int? locationid = delivery['locationid'] is int
        ? delivery['locationid'] as int
        : int.tryParse('${delivery['locationid'] ?? ''}');
    final int? orderheaderid = delivery['orderheaderid'] is int
        ? delivery['orderheaderid'] as int
        : int.tryParse('${delivery['orderheaderid'] ?? ''}');
    final int? deliveryid = delivery['deliveryid'] is int
        ? delivery['deliveryid'] as int
        : int.tryParse('${delivery['deliveryid'] ?? ''}');
    final int? userid = delivery['userid'] is int
        ? delivery['userid'] as int
        : int.tryParse('${delivery['userid'] ?? ''}');

    final String oid = (delivery['orderid'] ?? '').toString();
    final String ostatus = (delivery['orderstatus'] ?? '').toString();

    // Prefer deliverylat/long, fallback to droplat/lon
    final String lat = (delivery['deliverylat'] ?? delivery['droplat'] ?? '')
        .toString();
    final String lon = (delivery['deliverylong'] ?? delivery['droplon'] ?? '')
        .toString();

    if (tenantid != null) await prefs.setInt(kTenantId, tenantid);
    if (partnerid != null) await prefs.setInt(kPartnerId, partnerid);
    if (locationid != null) await prefs.setInt(kLocationId, locationid);
    if (orderheaderid != null) await prefs.setInt(kOrderHeaderId, orderheaderid);
    if (deliveryid != null) await prefs.setInt(kDeliveryId, deliveryid);
    if (userid != null) await prefs.setInt(kUserId, userid);
    if (oid.isNotEmpty) await prefs.setString(kOrderId, oid);
    if (ostatus.isNotEmpty) await prefs.setString(kOrderStatus, ostatus);
    if (lat.isNotEmpty) await prefs.setString(kLat, lat);
    if (lon.isNotEmpty) await prefs.setString(kLng, lon);

    // Update observables
    orderId.value = oid;
    orderStatus.value = ostatus;
    tenantId.value = tenantid ?? 0;
    partnerId.value = partnerid ?? 0;
    locationId.value = locationid ?? 0;
    orderHeaderId.value = orderheaderid ?? 0;
    deliveryId.value = deliveryid ?? 0;
    userId.value = userid ?? 0;
    latitude.value = lat;
    longitude.value = lon;
  }

  Future<void> loadFromPrefs() async {
    final prefs = await SharedPreferences.getInstance();
    tenantId.value = prefs.getInt(kTenantId) ?? 0;
    partnerId.value = prefs.getInt(kPartnerId) ?? 0;
    locationId.value = prefs.getInt(kLocationId) ?? 0;
    orderHeaderId.value = prefs.getInt(kOrderHeaderId) ?? 0;
    deliveryId.value = prefs.getInt(kDeliveryId) ?? 0;
    userId.value = prefs.getInt(kUserId) ?? 0;
    orderId.value = prefs.getString(kOrderId) ?? '';
    orderStatus.value = prefs.getString(kOrderStatus) ?? '';
    latitude.value = prefs.getString(kLat) ?? '';
    longitude.value = prefs.getString(kLng) ?? '';
  }

  Future<void> clear() async {
    final prefs = await SharedPreferences.getInstance();
    await prefs.remove(kTenantId);
    await prefs.remove(kPartnerId);
    await prefs.remove(kLocationId);
    await prefs.remove(kOrderHeaderId);
    await prefs.remove(kDeliveryId);
    await prefs.remove(kUserId);
    await prefs.remove(kOrderId);
    await prefs.remove(kOrderStatus);
    await prefs.remove(kLat);
    await prefs.remove(kLng);

    orderId.value = '';
    orderStatus.value = '';
    tenantId.value = 0;
    partnerId.value = 0;
    locationId.value = 0;
    orderHeaderId.value = 0;
    deliveryId.value = 0;
    userId.value = 0;
    latitude.value = '';
    longitude.value = '';
  }
}



