// models/table_order.dart
class TableOrder {
  final int id;
  final String nom;
  final int capacity;
  final String status; // 'available', 'occupied', 'reserved', 'maintenance'
  final String location;
  final DateTime createdAt;
  final DateTime updatedAt;
  final double? total; // Optionnel pour les commandes en cours
  final bool isEncashed;
  final String? time; // Heure de la commande si applicable
  final DateTime? date; // Date de la commande si applicable
  final String? tablename; // Date de la commande si applicable
  // final int? persons; // Nombre de personnes si applicable

  TableOrder({
    required this.id,
    required this.nom,
    required this.capacity,
    required this.status,
    required this.location,
    required this.createdAt,
    required this.updatedAt,
    this.total,
    this.isEncashed = false,
    this.time,
    this.date,
    this.tablename,
    // this.persons,
  });
factory TableOrder.fromJson(Map<String, dynamic> json) {
  return TableOrder(
    id: json['id'] ?? 0,
    nom: json['nom'] ?? '',
    capacity: json['capacity'] ?? 1,
    status: json['statut'] ?? 'available',
    location: json['location'] ?? '',
    tablename: json['tablename'] ?? '',
    createdAt: json['created_at'] != null
        ? DateTime.parse(json['created_at'])
        : DateTime.now(),
    updatedAt: json['updated_at'] != null
        ? DateTime.parse(json['updated_at'])
        : DateTime.now(),
    total: json['total_ht'] != null
        ? double.tryParse(json['total_ht'].toString())
        : null,
    isEncashed: json['is_encashed'] ?? false,
    time: json['time'],
    date: json['date_commande'] != null
        ? DateTime.parse(json['date_commande']) // tu avais mis updated_at ici par erreur
        : null,
  );
}


  Map<String, dynamic> toJson() {
    return {
      'id': id,
      'nom': nom,
      'capacity': capacity,
      'status': status,
      'location': location,
      'tablename': tablename,
      'created_at': createdAt.toIso8601String(),
      'updated_at': updatedAt.toIso8601String(),
      if (total != null) 'total': total,
      'is_encashed': isEncashed,
      if (time != null) 'time': time,
      if (date != null) 'date': date,
      // if (persons != null) 'persons': persons,
    };
  }

  // Getters pour la compatibilité avec l'ancien code
  int get tableNumber => id;
  String get tableName => nom;

  // Méthodes utilitaires
  bool get isAvailable => status == 'available';
  bool get isOccupied => status == 'occupied';
  bool get isReserved => status == 'reserved';
  bool get isInMaintenance => status == 'maintenance';

  // Méthode pour obtenir la couleur selon le statut
  String get statusColor {
    switch (status.toLowerCase()) {
      case 'available':
        return '#28A745'; // Vert
      case 'occupied':
        return '#DC3545'; // Rouge
      case 'reserved':
        return '#FFC107'; // Jaune
      case 'maintenance':
        return '#6C757D'; // Gris
      default:
        return '#007BFF'; // Bleu par défaut
    }
  }

  // Méthode pour obtenir le texte du statut en français
  String get statusText {
    switch (status.toLowerCase()) {
      case 'available':
        return 'Disponible';
      case 'occupied':
        return 'Occupée';
      case 'reserved':
        return 'Réservée';
      case 'maintenance':
        return 'Maintenance';
      default:
        return 'Inconnu';
    }
  }

  // Méthode pour créer une copie avec des modifications
  TableOrder copyWith({
    int? id,
    String? nom,
    int? capacity,
    String? status,
    String? location,
    DateTime? createdAt,
    DateTime? updatedAt,
    double? total,
    bool? isEncashed,
    String? time,
    DateTime? date,
    int? persons,
  }) {
    return TableOrder(
      id: id ?? this.id,
      nom: nom ?? this.nom,
      capacity: capacity ?? this.capacity,
      status: status ?? this.status,
      location: location ?? this.location,
      createdAt: createdAt ?? this.createdAt,
      updatedAt: updatedAt ?? this.updatedAt,
      total: total ?? this.total,
      isEncashed: isEncashed ?? this.isEncashed,
      time: time ?? this.time,
      date: date ?? this.date,
      // persons: persons ?? this.persons,
    );
  }

  @override
  String toString() {
    return 'TableOrder{id: $id, nom: $nom, capacity: $capacity, status: $status, location: $location}';
  }

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

  @override
  int get hashCode => id.hashCode;

  get items => null;

  where(bool Function(dynamic commande) param0) {}
}

// Énumération pour les statuts (optionnel, pour plus de type safety)
enum TableStatus {
  available,
  occupied,
  reserved,
  maintenance;

  String get displayName {
    switch (this) {
      case TableStatus.available:
        return 'Disponible';
      case TableStatus.occupied:
        return 'Occupée';
      case TableStatus.reserved:
        return 'Réservée';
      case TableStatus.maintenance:
        return 'Maintenance';
    }
  }

  String get color {
    switch (this) {
      case TableStatus.available:
        return '#28A745';
      case TableStatus.occupied:
        return '#DC3545';
      case TableStatus.reserved:
        return '#FFC107';
      case TableStatus.maintenance:
        return '#6C757D';
    }
  }
}

// Extension pour convertir string vers enum
extension TableStatusExtension on String {
  TableStatus get toTableStatus {
    switch (toLowerCase()) {
      case 'available':
        return TableStatus.available;
      case 'occupied':
        return TableStatus.occupied;
      case 'reserved':
        return TableStatus.reserved;
      case 'maintenance':
        return TableStatus.maintenance;
      default:
        return TableStatus.available;
    }
  }
}
