import 'dart:convert';

class CustomerResponseModel {
  final bool? success;
  final CustomerData? data;
  final dynamic errors;

  CustomerResponseModel({
    this.success,
    this.data,
    this.errors,
  });

  factory CustomerResponseModel.fromJson(String str) =>
      CustomerResponseModel.fromMap(json.decode(str));

  String toJson() => json.encode(toMap());

  factory CustomerResponseModel.fromMap(Map<String, dynamic> json) =>
      CustomerResponseModel(
        success: json["success"],
        data: json["data"] == null ? null : CustomerData.fromMap(json["data"]),
        errors: json["errors"],
      );

  Map<String, dynamic> toMap() => {
        "success": success,
        "data": data?.toMap(),
        "errors": errors,
      };
}

class CustomerData {
  final List<Customer>? customers;
  final int? totalCount;
  final int? page;
  final int? limit;
  final int? totalPages;

  CustomerData({
    this.customers,
    this.totalCount,
    this.page,
    this.limit,
    this.totalPages,
  });

  factory CustomerData.fromMap(Map<String, dynamic> json) => CustomerData(
        customers: json["data"] == null
            ? []
            : List<Customer>.from(json["data"].map((x) => Customer.fromMap(x))),
        totalCount: json["total_count"],
        page: json["page"],
        limit: json["limit"],
        totalPages: json["total_pages"],
      );

  Map<String, dynamic> toMap() => {
        "data": customers == null
            ? []
            : List<dynamic>.from(customers!.map((x) => x.toMap())),
        "total_count": totalCount,
        "page": page,
        "limit": limit,
        "total_pages": totalPages,
      };
}

class Customer {
  final String? id;
  final String? organizationId;
  final String? name;
  final String? email;
  final String? phone;
  final String? address;
  final bool? isDefault;
  final bool? isActive;
  final Map<String, dynamic>? metadata;
  final DateTime? createdAt;
  final DateTime? updatedAt;

  Customer({
    this.id,
    this.organizationId,
    this.name,
    this.email,
    this.phone,
    this.address,
    this.isDefault,
    this.isActive,
    this.metadata,
    this.createdAt,
    this.updatedAt,
  });

  factory Customer.fromMap(Map<String, dynamic> json) => Customer(
        id: json["id"],
        organizationId: json["organization_id"],
        name: json["name"],
        email: json["email"],
        phone: json["phone"],
        address: json["address"],
        isDefault: json["is_default"],
        isActive: json["is_active"],
        metadata: json["metadata"] ?? {},
        createdAt: json["created_at"] == null
            ? null
            : DateTime.parse(json["created_at"]),
        updatedAt: json["updated_at"] == null
            ? null
            : DateTime.parse(json["updated_at"]),
      );

  Map<String, dynamic> toMap() => {
        "id": id,
        "organization_id": organizationId,
        "name": name,
        "email": email,
        "phone": phone,
        "address": address,
        "is_default": isDefault,
        "is_active": isActive,
        "metadata": metadata,
        "created_at": createdAt?.toIso8601String(),
        "updated_at": updatedAt?.toIso8601String(),
      };
}
