import 'package:equatable/equatable.dart';

/// Repräsentiert einen Zählerstand für einen Objekttyp
/// Entspricht der Lua-Logik in CreateLoadingStockStartView etc.
class ObjectCounter extends Equatable {
  final String objectType; // z.B. 'meka', 'beka', 'hp1a', etc.
  final String label; // z.B. 'MEK', 'BEK', 'H1'
  final int currentCount; // Aktueller Bestand (z.B. im Fahrzeug)
  final int targetCount; // Soll-Zahl (z.B. Beladezähler)
  final int? alternateCount; // Alternative Zählung (z.B. HADAG, CR, SST)

  const ObjectCounter({
    required this.objectType,
    required this.label,
    required this.currentCount,
    required this.targetCount,
    this.alternateCount,
  });

  bool get isComplete => currentCount >= targetCount;
  bool get isOver => currentCount > targetCount;
  int get difference => targetCount - currentCount;

  @override
  List<Object?> get props => [objectType, label, currentCount, targetCount, alternateCount];
}

/// Gruppen von Zählern für verschiedene Ansichten
class CounterGroup extends Equatable {
  final String title;
  final List<ObjectCounter> counters;

  const CounterGroup({
    required this.title,
    required this.counters,
  });

  @override
  List<Object?> get props => [title, counters];
}

/// Zähler-Übersicht für eine komplette Tour/Page
class CounterOverview extends Equatable {
  final int tourId;
  final String? pageId;
  final List<CounterGroup> groups;
  final DateTime? lastUpdated;

  const CounterOverview({
    required this.tourId,
    this.pageId,
    required this.groups,
    this.lastUpdated,
  });

  /// Standard-Gruppen für StockStart (Lager Beladung)
  /// Entspricht Lua: CreateLoadingStockStartView
  factory CounterOverview.stockStart({
    required int tourId,
    required List<ObjectCounter> vehicleStock, // Bestand Fzg
    required List<ObjectCounter> loadingCounters, // Beladezähler
    required List<ObjectCounter> hadagCounters, // HADAG
    required List<ObjectCounter> sstCounters, // SST
    required List<ObjectCounter> crCounters, // CR
  }) {
    return CounterOverview(
      tourId: tourId,
      groups: [
        CounterGroup(title: 'Bestand Fzg', counters: vehicleStock),
        CounterGroup(title: 'Beladezähler', counters: loadingCounters),
        CounterGroup(title: 'HADAG', counters: hadagCounters),
        CounterGroup(title: 'SST', counters: sstCounters),
        CounterGroup(title: 'CR', counters: crCounters),
      ],
    );
  }

  /// Standard-Gruppen für VehStart
  /// Entspricht Lua: CreateLoadingVehStartView
  factory CounterOverview.vehStart({
    required int tourId,
    required List<ObjectCounter> loadingCounters,
  }) {
    return CounterOverview(
      tourId: tourId,
      groups: [
        CounterGroup(title: 'Beladezähler', counters: loadingCounters),
      ],
    );
  }

  /// Standard-Gruppen für Veh (Station)
  /// Entspricht Lua: CreateLoadingVehView
  factory CounterOverview.veh({
    required int tourId,
    String? pageId,
    required List<ObjectCounter> swapCounters, // Wechselzähler
    required List<ObjectCounter> pickupCounters, // Abholzähler
  }) {
    return CounterOverview(
      tourId: tourId,
      pageId: pageId,
      groups: [
        CounterGroup(title: 'Wechsel', counters: swapCounters),
        CounterGroup(title: 'Abholung', counters: pickupCounters),
      ],
    );
  }

  @override
  List<Object?> get props => [tourId, pageId, groups, lastUpdated];
}

/// Pickup/Abhol-Zähler aus der Datenbank
/// Entspricht Lua: page_pickup_count Tabelle
class PickupCount extends Equatable {
  final int tourId;
  final String pageId;
  final String objectType;
  final int count;

  const PickupCount({
    required this.tourId,
    required this.pageId,
    required this.objectType,
    required this.count,
  });

  @override
  List<Object?> get props => [tourId, pageId, objectType, count];
}

/// Swap/Wechsel-Zähler aus der Datenbank
/// Entspricht Lua: page_swap_count Tabelle
class SwapCount extends Equatable {
  final int tourId;
  final String pageId;
  final String objectType;
  final int count;

  const SwapCount({
    required this.tourId,
    required this.pageId,
    required this.objectType,
    required this.count,
  });

  @override
  List<Object?> get props => [tourId, pageId, objectType, count];
}

/// Container-Information für VS/Verwahrungsstelle
/// Entspricht Lua: ContainerId, ContainerType in vsStateMachine
class ContainerInfo extends Equatable {
  final String containerId;
  final String containerType; // 'a' = Geldinstitut, 'b' = Dienststelle
  final String? subtype; // 'cntra' oder 'cntrb'
  final int? objectCount; // Anzahl Objekte im Container

  const ContainerInfo({
    required this.containerId,
    required this.containerType,
    this.subtype,
    this.objectCount,
  });

  bool get isForGI => containerType == 'a';
  bool get isForDS => containerType == 'b';

  String get displayName {
    if (isForGI) return 'Container Geldinstitut';
    if (isForDS) return 'Container Dienststelle';
    return 'Container $containerId';
  }

  @override
  List<Object?> get props => [containerId, containerType, subtype, objectCount];
}

/// Letzte gescannte Objekte für die Anzeige
/// Entspricht Lua: Die Liste in ShowStockStartScreen etc.
class RecentScan extends Equatable {
  final String objectCode;
  final String objectName;
  final String state;
  final DateTime scanTime;
  final String? imageName;

  const RecentScan({
    required this.objectCode,
    required this.objectName,
    required this.state,
    required this.scanTime,
    this.imageName,
  });

  @override
  List<Object?> get props => [objectCode, objectName, state, scanTime, imageName];
}
