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

void main() {
  group('WendlerCalculator', () {
    const double bodyweight = 80.0;

    test('Week 1 (5/5/5+) generates correct percentages and reps', () {
      // Setup: Squat TM 100kg
      final sets = WendlerCalculator.generateSets(
        week: 1,
        trainingMax: 100.0,
        exerciseType: ExerciseType.squat,
        currentBodyweight: bodyweight,
      );

      expect(sets.length, 3);
      
      // Set 1: 65% = 65kg
      expect(sets[0].targetPercentage, 65);
      expect(sets[0].targetWeightTotal, 65.0);
      expect(sets[0].repsTarget, 5);
      expect(sets[0].isAmrap, false);

      // Set 2: 75% = 75kg
      expect(sets[1].targetPercentage, 75);
      expect(sets[1].targetWeightTotal, 75.0);
      expect(sets[1].repsTarget, 5);

      // Set 3: 85% = 85kg (AMRAP)
      expect(sets[2].targetPercentage, 85);
      expect(sets[2].targetWeightTotal, 85.0);
      expect(sets[2].repsTarget, 5);
      expect(sets[2].isAmrap, true);
    });

    test('Squat rounding works (2.5kg steps)', () {
      // 65% of 105kg = 68.25kg -> Should round down to 67.5kg
      final sets = WendlerCalculator.generateSets(
        week: 1,
        trainingMax: 105.0,
        exerciseType: ExerciseType.squat,
        currentBodyweight: bodyweight,
      );

      expect(sets[0].targetWeightTotal, 67.5);
    });

    test('Pullup calculation considers bodyweight', () {
      // TM 100kg, BW 80kg -> Target 65kg (65%)
      // Since Target (65) < BW (80), plate weight should be 0 (assistance/bodyweight logic handles negative)
      // But WendlerCalculator returns total system weight in targetWeightTotal
      
      final sets = WendlerCalculator.generateSets(
        week: 1,
        trainingMax: 100.0,
        exerciseType: ExerciseType.pullup,
        currentBodyweight: bodyweight,
      );

      expect(sets[0].targetWeightTotal, 65.0); // System weight
      expect(sets[0].plateWeight, 0.0); // No added weight
    });

    test('Pullup calculation with added weight', () {
      // TM 150kg -> 65% = 97.5kg
      // BW 80kg -> Added weight = 17.5kg
      
      final sets = WendlerCalculator.generateSets(
        week: 1,
        trainingMax: 150.0,
        exerciseType: ExerciseType.pullup,
        currentBodyweight: bodyweight,
      );

      expect(sets[0].targetWeightTotal, 97.5);
      expect(sets[0].plateWeight, 17.5);
    });

    test('FSL generates 5x5 at 65%', () {
      final fslSets = WendlerCalculator.generateFSLSets(
        trainingMax: 100.0,
        exerciseType: ExerciseType.squat,
        currentBodyweight: bodyweight,
      );

      expect(fslSets.length, 5);
      for (var set in fslSets) {
        expect(set.targetPercentage, 65);
        expect(set.targetWeightTotal, 65.0);
        expect(set.repsTarget, 5);
        expect(set.isAmrap, false);
      }
    });
  });
}
