import { describe, expect, it } from 'vitest';
import { generateTrainingPlan } from './trainingPlanGenerator';

const topWeaknesses = [
  { name: 'A', gapScore: 0.8, priorityScore: 0.9 },
  { name: 'B', gapScore: 0.6, priorityScore: 0.7 },
  { name: 'C', gapScore: 0.4, priorityScore: 0.5 }
];

const topStrengths = [
  { name: 'S1' },
  { name: 'S2' }
];

describe('generateTrainingPlan', () => {
  it('creates expected number of sessions for duration and cadence', () => {
    const plan = generateTrainingPlan({
      topWeaknesses,
      topStrengths,
      durationWeeks: 2,
      sessionsPerWeek: 3
    });

    expect(plan.sessions).toHaveLength(6);
    expect(plan.durationWeeks).toBe(2);
    expect(plan.sessionsPerWeek).toBe(3);
  });

  it('ensures each session has one main and two secondary exercises', () => {
    const plan = generateTrainingPlan({
      topWeaknesses,
      topStrengths,
      durationWeeks: 4,
      sessionsPerWeek: 2
    });

    for (const session of plan.sessions) {
      expect(session.mainExercise).toBeTruthy();
      expect(session.secondaryExercises).toHaveLength(2);

      const mainTasks = session.tasks.filter((task) => task.type === 'main');
      const secondaryTasks = session.tasks.filter((task) => task.type === 'secondary');

      expect(mainTasks).toHaveLength(1);
      expect(secondaryTasks).toHaveLength(2);
    }
  });

  it('rotates weighted main focus with 50/30/20 pattern', () => {
    const plan = generateTrainingPlan({
      topWeaknesses,
      topStrengths,
      durationWeeks: 6,
      sessionsPerWeek: 4
    });

    expect(plan.sessions).toHaveLength(24);

    const counts = plan.sessions.reduce(
      (accumulator, session) => {
        accumulator[session.mainExercise] = (accumulator[session.mainExercise] || 0) + 1;
        return accumulator;
      },
      { A: 0, B: 0, C: 0 }
    );

    expect(counts.A).toBe(12);
    expect(counts.B).toBe(7);
    expect(counts.C).toBe(5);
  });
});
