import 'dart:convert';

CustomerTenantsResponse customerTenantsResponseFromJson(String str) =>
    CustomerTenantsResponse.fromJson(json.decode(str));

String customerTenantsResponseToJson(CustomerTenantsResponse data) =>
    json.encode(data.toJson());

class CustomerTenantsResponse {
  final int? code;
  final List<Tenant>? details;
  final String? message;
  final bool? status;

  CustomerTenantsResponse({
    this.code,
    this.details,
    this.message,
    this.status,
  });

  factory CustomerTenantsResponse.fromJson(Map<String, dynamic> json) =>
      CustomerTenantsResponse(
        code: json["code"],
        details: json["details"] == null
            ? []
            : List<Tenant>.from(json["details"].map((x) => Tenant.fromJson(x))),
        message: json["message"],
        status: json["status"],
      );

  Map<String, dynamic> toJson() => {
    "code": code,
    "details": details == null
        ? []
        : List<dynamic>.from(details!.map((x) => x.toJson())),
    "message": message,
    "status": status,
  };
}

class Data {
  final Customer? customer;
  final List<Tenant>? tenants;

  Data({
    this.customer,
    this.tenants,
  });

  factory Data.fromJson(Map<String, dynamic> json) => Data(
    customer: json["customer"] == null
        ? null
        : Customer.fromJson(json["customer"]),
    tenants: json["tenants"] == null
        ? []
        : List<Tenant>.from(
        json["tenants"]!.map((x) => Tenant.fromJson(x))),
  );

  Map<String, dynamic> toJson() => {
    "customer": customer?.toJson(),
    "tenants": tenants == null
        ? []
        : List<dynamic>.from(tenants!.map((x) => x.toJson())),
  };
}

class Customer {
  final int? customerid;
  final String? firstname;
  final String? lastname;
  final String? profileimage;
  final String? gender;
  final String? dob;
  final String? dialcode;
  final String? contactno;
  final String? email;
  final String? deviceid;
  final String? devicetype;
  final int? authmode;
  final int? configid;
  final String? customertoken;
  final String? address;
  final String? suburb;
  final String? city;
  final String? state;
  final String? landmark;
  final String? doorno;
  final String? postcode;
  final String? latitude;
  final String? longitude;
  final int? applocationid;
  final int? status;
  final String? intro;

  Customer({
    this.customerid,
    this.firstname,
    this.lastname,
    this.profileimage,
    this.gender,
    this.dob,
    this.dialcode,
    this.contactno,
    this.email,
    this.deviceid,
    this.devicetype,
    this.authmode,
    this.configid,
    this.customertoken,
    this.address,
    this.suburb,
    this.city,
    this.state,
    this.landmark,
    this.doorno,
    this.postcode,
    this.latitude,
    this.longitude,
    this.applocationid,
    this.status,
    this.intro,
  });

  factory Customer.fromJson(Map<String, dynamic> json) => Customer(
    customerid: json["customerid"],
    firstname: json["firstname"],
    lastname: json["lastname"],
    profileimage: json["profileimage"],
    gender: json["gender"],
    dob: json["dob"],
    dialcode: json["dialcode"],
    contactno: json["contactno"],
    email: json["email"],
    deviceid: json["deviceid"],
    devicetype: json["devicetype"],
    authmode: json["authmode"],
    configid: json["configid"],
    customertoken: json["customertoken"],
    address: json["address"],
    suburb: json["suburb"],
    city: json["city"],
    state: json["state"],
    landmark: json["landmark"],
    doorno: json["doorno"],
    postcode: json["postcode"],
    latitude: json["latitude"],
    longitude: json["longitude"],
    applocationid: json["applocationid"],
    status: json["status"],
    intro: json["intro"],
  );

  Map<String, dynamic> toJson() => {
    "customerid": customerid,
    "firstname": firstname,
    "lastname": lastname,
    "profileimage": profileimage,
    "gender": gender,
    "dob": dob,
    "dialcode": dialcode,
    "contactno": contactno,
    "email": email,
    "deviceid": deviceid,
    "devicetype": devicetype,
    "authmode": authmode,
    "configid": configid,
    "customertoken": customertoken,
    "address": address,
    "suburb": suburb,
    "city": city,
    "state": state,
    "landmark": landmark,
    "doorno": doorno,
    "postcode": postcode,
    "latitude": latitude,
    "longitude": longitude,
    "applocationid": applocationid,
    "status": status,
    "intro": intro,
  };
}

class Tenant {
  final int? tenantid;
  final String? tenantname;
  final String? tenanttoken;
  final String? tenantbanner;
  final double? tenantcharge;
  final String? address;
  final String? licenseno;
  final String? primaryemail;
  final String? primarycontact;
  final int? pickuplocationid;
  final int? applocationid;
  final String? suburb;
  final String? city;
  final String? latitude;
  final String? longitude;
  final String? postcode;
  final String? tenantimage;
  final int? locationid;
  final String? locationname;
  final int? subcategoryid;
  final int? categoryid;
  final String? registrationno;
  final int? orderscount;
  final List<Subcategory>? subcategories;

  Tenant({
    this.tenantid,
    this.tenantname,
    this.tenanttoken,
    this.tenantbanner,
    this.tenantcharge,
    this.address,
    this.licenseno,
    this.primaryemail,
    this.primarycontact,
    this.pickuplocationid,
    this.applocationid,
    this.suburb,
    this.city,
    this.latitude,
    this.longitude,
    this.postcode,
    this.tenantimage,
    this.locationid,
    this.locationname,
    this.subcategoryid,
    this.categoryid,
    this.registrationno,
    this.orderscount,
    this.subcategories,
  });

  factory Tenant.fromJson(Map<String, dynamic> json) => Tenant(
    tenantid: json["tenantid"],
    tenantname: json["tenantname"],
    tenanttoken: json["userfcmtoken"],
    tenantbanner: json["tenantbanner"],
    tenantcharge: json["tenantcharge"],
    address: json["address"],
    licenseno: json["licenseno"],
    primaryemail: json["primaryemail"],
    primarycontact: json["primarycontact"],
    pickuplocationid: json["pickuplocationid"],
    applocationid: json["applocationid"],
    suburb: json["suburb"],
    city: json["city"],
    latitude: json["latitude"],
    longitude: json["longitude"],
    postcode: json["postcode"],
    tenantimage: json["tenantimage"],
    locationid: json["locationid"],
    locationname: json["locationname"],
    subcategoryid: json["subcategoryid"],
    categoryid: json["categoryid"],
    registrationno: json["registrationno"],
    orderscount: json["orderscount"],
    subcategories: json["productsubcategory"] == null
        ? []
        : List<Subcategory>.from(
        json["productsubcategory"].map((x) => Subcategory.fromJson(x))),
  );

  Map<String, dynamic> toJson() => {
    "tenantid": tenantid,
    "tenantname": tenantname,
    "userfcmtoken": tenanttoken,
    "tenantbanner": tenantbanner,
    "tenantchanrge": tenantcharge,
    "address": address,
    "licenseno": licenseno,
    "primaryemail": primaryemail,
    "primarycontact": primarycontact,
    "pickuplocationid": pickuplocationid,
    "applocationid": applocationid,
    "suburb": suburb,
    "city": city,
    "latitude": latitude,
    "longitude": longitude,
    "postcode": postcode,
    "tenantimage": tenantimage,
    "locationid": locationid,
    "locationname": locationname,
    "subcategoryid": subcategoryid,
    "categoryid": categoryid,
    "registrationno": registrationno,
    "orderscount": orderscount,
    "productsubcategory": subcategories == null
        ? []
        : List<dynamic>.from(subcategories!.map((x) => x.toJson())),
  };
}

class Subcategory {
  final int? subcatid;
  final int? categoryid;
  final int? tenantid;
  final String? subcatname;
  final String? status;
  final String? image;

  Subcategory({
    this.subcatid,
    this.categoryid,
    this.tenantid,
    this.subcatname,
    this.status,
    this.image,
  });

  factory Subcategory.fromJson(Map<String, dynamic> json) => Subcategory(
    subcatid: json["subcatid"],
    categoryid: json["categoryid"],
    tenantid: json["tenantid"],
    subcatname: json["subcatname"],
    status: json["status"],
    image: json["image"],
  );

  Map<String, dynamic> toJson() => {
    "subcatid": subcatid,
    "categoryid": categoryid,
    "tenantid": tenantid,
    "subcatname": subcatname,
    "status": status,
    "image": image,
  };
}



OrdersResponse ordersResponseFromJson(String str) =>
    OrdersResponse.fromJson(json.decode(str));

String ordersResponseToJson(OrdersResponse data) =>
    json.encode(data.toJson());

class OrdersResponse {
  final int? code;
  final List<OrderDatum>? data;
  final String? message;
  final bool? status;

  OrdersResponse({
    this.code,
    this.data,
    this.message,
    this.status,
  });

  factory OrdersResponse.fromJson(Map<String, dynamic> json) =>
      OrdersResponse(
        code: json["code"],
        data: json["data"] == null
            ? []
            : List<OrderDatum>.from(
            json["data"].map((x) => OrderDatum.fromJson(x))),
        message: json["message"],
        status: json["status"],
      );

  Map<String, dynamic> toJson() => {
    "code": code,
    "data": data == null
        ? []
        : List<dynamic>.from(data!.map((x) => x.toJson())),
    "message": message,
    "status": status,
  };
}

class OrderDatum {
  final int? orderheaderid;
  final String? gstno;
  final String? orderid;
  final String? orderstatus;
  final DateTime? orderdate;
  final int? itemcount;
  final double? deliverycharge;
  final double? orderamount;
  final double? taxamount;
  final double? totaltaxamount;
  final String? tenantname;
  final String? tenantsuburb;
  final String? tenantcity;
  final String? deliveryaddress;
  final String? deliverystatus;
  final String? pickupaddress;
  final String? pickupcustomer;
  final String? pickupcontactno;
  final String? deliverycustomer;
  final String? deliverycontactno;
  final String? locationname;
  final String? locationcity;
  final String? tenantimage;
  final List<OrderDetail>? orderdetails;

  OrderDatum({
    this.orderheaderid,
    this.orderid,
    this.gstno,
    this.orderstatus,
    this.orderdate,
    this.itemcount,
    this.deliverycharge,
    this.orderamount,
    this.taxamount,
    this.totaltaxamount,
    this.tenantname,
    this.tenantsuburb,
    this.tenantcity,
    this.deliveryaddress,
    this.deliverystatus,
    this.pickupaddress,
    this.pickupcustomer,
    this.pickupcontactno,
    this.deliverycustomer,
    this.deliverycontactno,
    this.locationname,
    this.locationcity,
    this.orderdetails,
    this.tenantimage,
  });

  factory OrderDatum.fromJson(Map<String, dynamic> json) => OrderDatum(
    orderheaderid: json["orderheaderid"],
    orderid: json["orderid"],
    gstno: json["registrationno"],
    orderstatus: json["orderstatus"],
    orderdate:
    json["orderdate"] == null ? null : DateTime.parse(json["orderdate"]),
    itemcount: json["itemcount"],
    deliverycharge: json["deliverycharge"]?.toDouble(),
    orderamount: json["orderamount"]?.toDouble(),
    taxamount: json["taxamount"]?.toDouble(),
    totaltaxamount: json["totaltaxamount"]?.toDouble(),
    tenantname: json["tenantname"],
    tenantsuburb: json["tenantsuburb"],
    tenantcity: json["tenantcity"],
    deliveryaddress: json["deliveryaddress"],
    deliverystatus: json["deliverystatus"],
    pickupaddress: json["pickupaddress"],
    pickupcustomer: json["pickupcustomer"],
    pickupcontactno: json["pickupcontactno"],
    deliverycustomer: json["deliverycustomer"],
    deliverycontactno: json["deliverycontactno"],
    locationname: json["locationname"],
    tenantimage: json["tenantimage"],
    locationcity: json["locationcity"],
    orderdetails: json["orderdetails"] == null
        ? []
        : List<OrderDetail>.from(
        json["orderdetails"].map((x) => OrderDetail.fromJson(x))),
  );

  Map<String, dynamic> toJson() => {
    "orderheaderid": orderheaderid,
    "orderid": orderid,
    "gstno": gstno,
    "orderstatus": orderstatus,
    "orderdate": orderdate?.toIso8601String(),
    "itemcount": itemcount,
    "deliverycharge": deliverycharge,
    "orderamount": orderamount,
    "taxamount": taxamount,
    "totaltaxamount": totaltaxamount,
    "tenantname": tenantname,
    "tenantsuburb": tenantsuburb,
    "tenantcity": tenantcity,
    "deliveryaddress": deliveryaddress,
    "deliverystatus": deliverystatus,
    "pickupaddress": pickupaddress,
    "pickupcustomer": pickupcustomer,
    "pickupcontactno": pickupcontactno,
    "deliverycustomer": deliverycustomer,
    "deliverycontactno": deliverycontactno,
    "locationname": locationname,
    "locationcity": locationcity,
    "orderdetails": orderdetails == null
        ? []
        : List<dynamic>.from(orderdetails!.map((x) => x.toJson())),
  };
}

class OrderDetail {
  final int? orderdetailid;
  final int? orderheaderid;
  final int? productid;
  final String? productname;
  final String? productdescription;
  final int? orderqty;
  final double? price;
  final String? unitname;
  final double? productsumprice;
  final String? productimage;

  OrderDetail({
    this.orderdetailid,
    this.orderheaderid,
    this.productid,
    this.productname,
    this.productdescription,
    this.orderqty,
    this.price,
    this.unitname,
    this.productsumprice,
    this.productimage,
  });

  factory OrderDetail.fromJson(Map<String, dynamic> json) => OrderDetail(
    orderdetailid: json["orderdetailid"],
    orderheaderid: json["orderheaderid"],
    productid: json["productid"],
    productname: json["productname"],
    productdescription: json["productdescription"],
    orderqty: json["orderqty"],
    price: json["price"]?.toDouble(),
    unitname: json["unitname"],
    productsumprice: json["productsumprice"]?.toDouble(),
    productimage: json["productimage"],
  );

  Map<String, dynamic> toJson() => {
    "orderdetailid": orderdetailid,
    "orderheaderid": orderheaderid,
    "productid": productid,
    "productname": productname,
    "productdescription": productdescription,
    "orderqty": orderqty,
    "price": price,
    "unitname": unitname,
    "productsumprice": productsumprice,
    "productimage": productimage,
  };
}
class TenantLocation {
  final int locationId;
  final int tenantId;
  final int applocationId;
  final int moduleId;
  final int roleId;
  final String locationName;
  final String email;
  final String contactNo;
  final String latitude;
  final String longitude;
  final String address;
  final String suburb;
  final String city;
  final String state;
  final String postcode;
  final String openTime;
  final String closeTime;
  final int partnerId;
  final int deliveryRadius;
  final int deliveryMins;
  final int cancelSecs;
  final String status;

  TenantLocation({
    required this.locationId,
    required this.tenantId,
    required this.applocationId,
    required this.moduleId,
    required this.roleId,
    required this.locationName,
    required this.email,
    required this.contactNo,
    required this.latitude,
    required this.longitude,
    required this.address,
    required this.suburb,
    required this.city,
    required this.state,
    required this.postcode,
    required this.openTime,
    required this.closeTime,
    required this.partnerId,
    required this.deliveryRadius,
    required this.deliveryMins,
    required this.cancelSecs,
    required this.status,
  });

  factory TenantLocation.fromJson(Map<String, dynamic> json) {
    return TenantLocation(
      locationId: json['locationid'] ?? 0,
      tenantId: json['tenantid'] ?? 0,
      applocationId: json['applocationid'] ?? 0,
      moduleId: json['moduleid'] ?? 0,
      roleId: json['roleid'] ?? 0,
      locationName: json['locationname'] ?? '',
      email: json['email'] ?? '',
      contactNo: json['contactno'] ?? '',
      latitude: json['latitude'] ?? '',
      longitude: json['longitude'] ?? '',
      address: json['address'] ?? '',
      suburb: json['suburb'] ?? '',
      city: json['city'] ?? '',
      state: json['state'] ?? '',
      postcode: json['postcode'] ?? '',
      openTime: json['opentime'] ?? '',
      closeTime: json['closetime'] ?? '',
      partnerId: json['partnerid'] ?? 0,
      deliveryRadius: json['deliveryradius'] ?? 0,
      deliveryMins: json['deliverymins'] ?? 0,
      cancelSecs: json['cancelsecs'] ?? 0,
      status: json['status'] ?? '',
    );
  }

  Map<String, dynamic> toJson() {
    return {
      'locationid': locationId,
      'tenantid': tenantId,
      'applocationid': applocationId,
      'moduleid': moduleId,
      'roleid': roleId,
      'locationname': locationName,
      'email': email,
      'contactno': contactNo,
      'latitude': latitude,
      'longitude': longitude,
      'address': address,
      'suburb': suburb,
      'city': city,
      'state': state,
      'postcode': postcode,
      'opentime': openTime,
      'closetime': closeTime,
      'partnerid': partnerId,
      'deliveryradius': deliveryRadius,
      'deliverymins': deliveryMins,
      'cancelsecs': cancelSecs,
      'status': status,
    };
  }
}

class TenantLocationsResponse {
  final int code;
  final List<TenantLocation> details;
  final String message;
  final bool status;

  TenantLocationsResponse({
    required this.code,
    required this.details,
    required this.message,
    required this.status,
  });

  factory TenantLocationsResponse.fromJson(Map<String, dynamic> json) {
    final listJson = json['details'] as List<dynamic>?; // safer cast
    final detailsList = listJson != null
        ? listJson.map((e) => TenantLocation.fromJson(e as Map<String, dynamic>)).toList()
        : <TenantLocation>[];

    return TenantLocationsResponse(
      code: json['code'] ?? 0,
      details: detailsList,
      message: json['message'] ?? '',
      status: json['status'] ?? true, // default true if API doesn't send
    );
  }


  Map<String, dynamic> toJson() {
    return {
      'code': code,
      'details': details.map((e) => e.toJson()).toList(),
      'message': message,
      'status': status,
    };
  }
}
