class Liste {
  final String id;
  String title;
  final String owner;
  List<String> members;

  Liste({
    required this.id,
    required this.title,
    required this.owner,
    required this.members,
  });

  factory Liste.fromJson(Map<String, dynamic> json) {
    return Liste(
      id: json['id'] as String,
      title: json['title'] as String,
      owner: json['owner'] as String? ?? '',
      members: List<String>.from(json['members'] as List? ?? []),
    );
  }
}

class Item {
  final String id;
  String name;
  bool checked;
  int position;

  Item({
    required this.id,
    required this.name,
    this.checked = false,
    this.position = 0,
  });

  factory Item.fromJson(Map<String, dynamic> json) {
    return Item(
      id: json['id'] as String,
      name: json['name'] as String,
      checked: json['checked'] as bool? ?? false,
      position: (json['position'] is int)
          ? json['position']
          : int.tryParse(json['position'].toString()) ?? 0,
    );
  }
}

class StoreLayout {
  final String id;
  String name;
  String owner;
  double? latitude;
  double? longitude;
  String? address;
  bool isPublic;

  StoreLayout({
    required this.id,
    required this.name,
    required this.owner,
    this.latitude,
    this.longitude,
    this.address,
    this.isPublic = false,
  });

  factory StoreLayout.fromJson(Map<String, dynamic> json) {
    double? parseCoord(dynamic value) {
      if (value == null) return null;
      if (value is double) return value;
      if (value is int) return value.toDouble();
      return double.tryParse(value.toString());
    }

    return StoreLayout(
      id: json['id'] as String,
      name: json['name'] as String? ?? '',
      owner: json['owner'] as String? ?? '',
      latitude: parseCoord(json['latitude']),
      longitude: parseCoord(json['longitude']),
      address: json['address'] as String?,
      isPublic: json['isPublic'] as bool? ?? false,
    );
  }
}

class StoreSection {
  final String id;
  final String layoutId;
  String name;
  int position;
  String? category;

  StoreSection({
    required this.id,
    required this.layoutId,
    required this.name,
    this.position = 0,
    this.category,
  });

  factory StoreSection.fromJson(Map<String, dynamic> json) {
    int parsePosition(dynamic value) {
      if (value is int) return value;
      return int.tryParse(value.toString()) ?? 0;
    }

    return StoreSection(
      id: json['id'] as String,
      layoutId: json['layout'] as String? ?? '',
      name: json['name'] as String? ?? '',
      position: parsePosition(json['position']),
      category: json['category'] as String?,
    );
  }
}
