class MenuCategory {
  final int id;
  final String nom;
  final String? description;
  final int? ordre;
  final bool actif;

  MenuCategory({
    required this.id,
    required this.nom,
    this.description,
    this.ordre,
    required this.actif,
  });

  factory MenuCategory.fromJson(Map<String, dynamic> json) {
    return MenuCategory(
      id: json['id'],
      nom: json['nom'],
      description: json['description'],
      ordre: json['ordre'],
      actif: json['actif'] ?? true,
    );
  }

  @override
  bool operator ==(Object other) =>
      identical(this, other) ||
      other is MenuCategory &&
          runtimeType == other.runtimeType &&
          id == other.id;

  @override
  int get hashCode => id.hashCode;
}

class MenuPlat {
  final int id;
  final String nom;
  final String? commentaire;
  final double prix;
  final String? ingredients;
  final String? imageUrl;
  final bool disponible;
  final MenuCategory? category; // Single category pour la compatibilité API
  final List<MenuCategory>? categories; // Multiple categories si besoin

  MenuPlat({
    required this.id,
    required this.nom,
    this.commentaire,
    required this.prix,
    this.ingredients,
    this.imageUrl,
    required this.disponible,
    this.category,
    this.categories,
  });

  factory MenuPlat.fromJson(Map<String, dynamic> json) {
    double parsePrix(dynamic p) {
      if (p is int) return p.toDouble();
      if (p is double) return p;
      if (p is String) return double.tryParse(p) ?? 0;
      return 0;
    }

    return MenuPlat(
      id: json['id'],
      nom: json['nom'],
      commentaire: json['commentaire'],
      prix: parsePrix(json['prix']),
      ingredients: json['ingredients'],
      imageUrl: json['image_url'],
      disponible: json['disponible'] ?? true,
      // Support pour single category (API actuelle)
      category: json['category'] != null
          ? MenuCategory.fromJson(json['category'])
          : null,
      // Support pour multiple categories si l'API évolue
      categories: json['categories'] != null
          ? (json['categories'] as List)
              .map((c) => MenuCategory.fromJson(c))
              .toList()
          : null,
    );
  }
}
