class ChartDataPoint {
  final int stopNumber;
  final double litersPerStop;
  final double pricePerStop;
  final double pricePerLiter;

  ChartDataPoint({
    required this.stopNumber,
    required this.litersPerStop,
    required this.pricePerStop,
    required this.pricePerLiter,
  });
}

class ChartData {
  final List<ChartDataPoint> dataPoints;

  ChartData({required this.dataPoints});

  factory ChartData.fromTankList(List<dynamic> tankList) {
    List<ChartDataPoint> points = [];

    for (int i = 0; i < tankList.length; i++) {
      final tank = tankList[i];
      final liters = double.tryParse(tank.szLiters) ?? 0.0;
      final priceTotal = double.tryParse(tank.szPriceTotal) ?? 0.0;
      final pricePerLiter = double.tryParse(tank.szPricePerLiter) ?? 0.0;

      points.add(
        ChartDataPoint(
          stopNumber: i + 1,
          litersPerStop: liters,
          pricePerStop: priceTotal,
          pricePerLiter: pricePerLiter,
        ),
      );
    }

    return ChartData(dataPoints: points);
  }

  double getMaxLiters() {
    if (dataPoints.isEmpty) return 0;
    return dataPoints
        .map((p) => p.litersPerStop)
        .reduce((a, b) => a > b ? a : b);
  }

  double getMaxPrice() {
    if (dataPoints.isEmpty) return 0;
    return dataPoints
        .map((p) => p.pricePerStop)
        .reduce((a, b) => a > b ? a : b);
  }

  double getMaxPricePerLiter() {
    if (dataPoints.isEmpty) return 0;
    return dataPoints
        .map((p) => p.pricePerLiter)
        .reduce((a, b) => a > b ? a : b);
  }
}
