import { describe, it, expect } from 'vitest';
import { buildPrefillExercises, type AiWorkoutDraft } from '@/lib/ai/workoutDraft';

// Minimal library shape buildPrefillExercises needs (id + cardio inputs).
const lib = [
  { id: 'press', type: 'barbell', muscleGroups: '["shoulders"]' },
  { id: 'bike', type: 'cardio', muscleGroups: '[]' },
  // Tagged cardio via muscleGroups even though the equipment type isn't.
  { id: 'boxjump', type: 'bodyweight', muscleGroups: '["legs","cardio"]' },
];

describe('buildPrefillExercises', () => {
  it('expands a strength exercise into N sets with weight+reps and RPE only', () => {
    const draft: AiWorkoutDraft = {
      name: 'Push',
      exercises: [
        {
          exerciseId: 'press',
          sets: 4,
          reps: 6,
          suggestedWeight: 95,
          rpe: 8,
          gear: 3, // wrong-kind value — must be dropped for non-cardio
        },
      ],
    };
    const [ex] = buildPrefillExercises(draft, lib);
    expect(ex.sets).toHaveLength(4);
    expect(ex.sets.map((s) => s.setNumber)).toEqual([1, 2, 3, 4]);
    for (const s of ex.sets) {
      expect(s.weight).toBe(95);
      expect(s.reps).toBe(6);
      expect(s.rpe).toBe(8);
      expect(s.gear).toBeUndefined();
    }
  });

  it('uses gear (not rpe) for a cardio exercise', () => {
    const draft: AiWorkoutDraft = {
      name: 'Conditioning',
      exercises: [
        { exerciseId: 'bike', sets: 1, durationSeconds: 600, gear: 3, rpe: 8 },
      ],
    };
    const [ex] = buildPrefillExercises(draft, lib);
    expect(ex.sets[0].gear).toBe(3);
    expect(ex.sets[0].rpe).toBeUndefined();
    expect(ex.sets[0].durationSeconds).toBe(600);
  });

  it('treats an exercise tagged "cardio" in muscleGroups as cardio', () => {
    const draft: AiWorkoutDraft = {
      name: 'Plyo',
      exercises: [{ exerciseId: 'boxjump', sets: 3, reps: 5, rpe: 7, gear: 2 }],
    };
    const [ex] = buildPrefillExercises(draft, lib);
    expect(ex.sets[0].gear).toBe(2);
    expect(ex.sets[0].rpe).toBeUndefined();
  });

  it('defaults to 3 sets when the count is missing or non-positive', () => {
    const draft: AiWorkoutDraft = {
      name: 'X',
      exercises: [
        { exerciseId: 'press', sets: 0, reps: 5, suggestedWeight: 100 },
      ],
    };
    const [ex] = buildPrefillExercises(draft, lib);
    expect(ex.sets).toHaveLength(3);
  });

  it('drops exercises whose id is not in the library', () => {
    const draft: AiWorkoutDraft = {
      name: 'X',
      exercises: [
        { exerciseId: 'ghost', sets: 3, reps: 5 },
        { exerciseId: 'press', sets: 2, reps: 5, suggestedWeight: 100 },
      ],
    };
    const out = buildPrefillExercises(draft, lib);
    expect(out).toHaveLength(1);
    expect(out[0].exercise.id).toBe('press');
  });

  it('puts the coaching note on the first set only', () => {
    const draft: AiWorkoutDraft = {
      name: 'X',
      exercises: [
        {
          exerciseId: 'press',
          sets: 3,
          reps: 5,
          suggestedWeight: 100,
          notes: 'brace hard',
        },
      ],
    };
    const [ex] = buildPrefillExercises(draft, lib);
    expect(ex.sets[0].notes).toBe('brace hard');
    expect(ex.sets[1].notes).toBeUndefined();
    expect(ex.sets[2].notes).toBeUndefined();
  });
});
