import { describe, expect, it } from 'vitest';
import { buildAnalysis } from './analysisEngine';

const createExercise = (name, soll, faktor, durchschnitt) => ({
  name,
  soll,
  faktor,
  durchschnitt,
  points: durchschnitt * faktor
});

const createAssessment = (id, datum, exercises) => ({
  id,
  patType: 'PAT 1',
  datum,
  name: `Spieler ${id}`,
  exercises
});

describe('buildAnalysis', () => {
  it('uses the latest 5 assessments for windowing', () => {
    const baseExercises = [
      createExercise('1) Faktor hoch konstant', 10, 40, 5),
      createExercise('2) Faktor mittel konstant', 10, 20, 5),
      createExercise('3) Faktor hoch schwankend', 10, 40, 5),
      createExercise('4) Stärke', 5, 10, 7)
    ];

    const assessments = [
      createAssessment('a1', '2026-02-01', baseExercises),
      createAssessment('a2', '2026-02-02', baseExercises),
      createAssessment('a3', '2026-02-03', baseExercises),
      createAssessment('a4', '2026-02-04', baseExercises),
      createAssessment('a5', '2026-02-05', baseExercises),
      createAssessment('a6', '2026-02-06', baseExercises)
    ];

    const result = buildAnalysis(assessments, 'PAT 1', { windowSize: 5, missingStrategy: 'zero' });

    expect(result.windowedAssessments).toHaveLength(5);
    expect(result.windowedAssessments[0].id).toBe('a6');
    expect(result.windowedAssessments[4].id).toBe('a2');
    expect(result.isLimitedData).toBe(false);
  });

  it('fills missing exercise values with zero and marks limited data when less than window', () => {
    const assessmentA = createAssessment('a1', '2026-02-03', [
      createExercise('1) Technik', 10, 30, 6)
    ]);

    const assessmentB = createAssessment('a2', '2026-02-04', []);

    const assessmentC = createAssessment('a3', '2026-02-05', [
      createExercise('1) Technik', 10, 30, 4)
    ]);

    const result = buildAnalysis([assessmentA, assessmentB, assessmentC], 'PAT 1', {
      windowSize: 5,
      missingStrategy: 'zero'
    });

    expect(result.isLimitedData).toBe(true);
    expect(result.exerciseMetrics[0].actualSeries).toContain(0);
  });

  it('prioritizes weaknesses by gap x factor x consistency and detects strengths', () => {
    const rows = [
      createAssessment('a1', '2026-02-01', [
        createExercise('1) Faktor hoch konstant', 10, 40, 5),
        createExercise('2) Faktor mittel konstant', 10, 20, 5),
        createExercise('3) Faktor hoch schwankend', 10, 40, 0),
        createExercise('4) Stärke', 5, 10, 7)
      ]),
      createAssessment('a2', '2026-02-02', [
        createExercise('1) Faktor hoch konstant', 10, 40, 5),
        createExercise('2) Faktor mittel konstant', 10, 20, 5),
        createExercise('3) Faktor hoch schwankend', 10, 40, 10),
        createExercise('4) Stärke', 5, 10, 7)
      ]),
      createAssessment('a3', '2026-02-03', [
        createExercise('1) Faktor hoch konstant', 10, 40, 5),
        createExercise('2) Faktor mittel konstant', 10, 20, 5),
        createExercise('3) Faktor hoch schwankend', 10, 40, 0),
        createExercise('4) Stärke', 5, 10, 7)
      ]),
      createAssessment('a4', '2026-02-04', [
        createExercise('1) Faktor hoch konstant', 10, 40, 5),
        createExercise('2) Faktor mittel konstant', 10, 20, 5),
        createExercise('3) Faktor hoch schwankend', 10, 40, 10),
        createExercise('4) Stärke', 5, 10, 7)
      ]),
      createAssessment('a5', '2026-02-05', [
        createExercise('1) Faktor hoch konstant', 10, 40, 5),
        createExercise('2) Faktor mittel konstant', 10, 20, 5),
        createExercise('3) Faktor hoch schwankend', 10, 40, 5),
        createExercise('4) Stärke', 5, 10, 7)
      ])
    ];

    const result = buildAnalysis(rows, 'PAT 1', { windowSize: 5, missingStrategy: 'zero' });

    expect(result.topWeaknesses[0].name).toBe('3) Faktor hoch schwankend');
    expect(result.topWeaknesses[1].name).toBe('1) Faktor hoch konstant');
    expect(result.topWeaknesses[2].name).toBe('2) Faktor mittel konstant');

    expect(result.topStrengths[0].name).toBe('4) Stärke');
    expect(result.topStrengths[0].strengthScore).toBeGreaterThan(0);
  });
});
