import 'package:flutter_test/flutter_test.dart';
import 'package:slrpg_app/src/shared/domain/logic/plate_calculator.dart';

void main() {
  group('PlateCalculator', () {
    final plates = [20.0, 10.0, 5.0, 2.5, 1.25];
    final bands = {'Green': 20.0, 'Blue': 10.0};

    test('Calculates barbell plates correctly', () {
      // Target 70kg, Bar 20kg -> Need 50kg -> 25kg per side -> 20 + 5
      final result = PlateCalculator.calculate(
        targetWeight: 70.0,
        barWeight: 20.0,
        availablePlates: plates,
        isTwoSided: true,
      );

      expect(result.success, true);
      expect(result.plateConfiguration, [20.0, 5.0]);
      expect(result.totalAchieved, 70.0);
    });

    test('Calculates belt weight correctly (One Sided)', () {
      // Need 30kg added -> 20 + 10
      final result = PlateCalculator.calculate(
        targetWeight: 110.0, // 80 BW + 30 Added
        barWeight: 80.0,     // BW
        availablePlates: plates,
        isTwoSided: false,
      );

      expect(result.success, true);
      expect(result.plateConfiguration, [20.0, 10.0]);
      expect(result.totalAchieved, 110.0);
    });

    test('Band Selection: Correct band chosen', () {
      // Need -10kg support
      final result = PlateCalculator.calculate(
        targetWeight: 70.0,
        barWeight: 80.0, // BW
        availablePlates: plates,
        availableBands: bands,
        isTwoSided: false,
      );

      expect(result.success, true);
      expect(result.plateConfiguration, isEmpty);
      expect(result.bandAssistance, 'Blue'); // 10kg band
      expect(result.totalAchieved, 70.0);
    });

    test('Smart Switch: Bodyweight is better than oversized band', () {
      // Need -2kg support. Smallest band is 10kg.
      // With band: -10kg (8kg error). Without band: 0kg (2kg error).
      // Should choose Bodyweight Only.
      
      final result = PlateCalculator.calculate(
        targetWeight: 78.0,
        barWeight: 80.0,
        availablePlates: plates,
        availableBands: {'BigBand': 10.0},
        isTwoSided: false,
      );

      expect(result.success, true);
      expect(result.bandAssistance, null); // No band!
      expect(result.message, contains('Bodyweight Only'));
    });

    test('Rounding Logic handles uneven weights', () {
      // Need 9kg. Smallest plate 1.25.
      // 9 / 1.25 = 7.2. Should round to 7 * 1.25 = 8.75kg or 8 * 1.25 = 10kg.
      // 8.75 is closer (0.25 diff) than 10 (1.0 diff).
      
      final result = PlateCalculator.calculate(
        targetWeight: 29.0,
        barWeight: 20.0,
        availablePlates: [1.25], // Only small plates
        isTwoSided: false, // Belt
      );

      // Needed 9.0. Found 8.75 (7x 1.25). Total 28.75.
      expect(result.success, true);
      expect(result.totalAchieved, 28.75);
      expect(result.plateConfiguration.length, 7);
    });
  });
}
