import { describe, it, expect } from "vitest";
import {
  computeAverageScore,
  generateFindings,
  generateRecommendations,
  evaluationToCsvRows,
} from "./export-utils";

describe("computeAverageScore", () => {
  it("returns 0 for empty array", () => {
    expect(computeAverageScore([])).toBe(0);
  });

  it("returns 0 when all scores are null", () => {
    expect(computeAverageScore([{ score: null }, { score: null }])).toBe(0);
  });

  it("computes average of valid scores", () => {
    expect(computeAverageScore([{ score: 2 }, { score: 4 }])).toBe(3);
  });

  it("ignores null and out-of-range scores", () => {
    expect(computeAverageScore([{ score: 2 }, { score: null }, { score: 4 }, { score: 0 }])).toBe(3);
  });
});

describe("generateFindings", () => {
  const mkDim = (name: string) => ({ name });
  const mkScore = (score: number, name: string) => ({ score, dimension: mkDim(name) });

  it("returns message when no scores", () => {
    expect(generateFindings([])).toContain("Aucun score");
  });

  it("includes average and strengths", () => {
    const scores = [mkScore(4, "A"), mkScore(5, "B")];
    const out = generateFindings(scores);
    expect(out).toContain("4.5/5");
    expect(out).toContain("A");
    expect(out).toContain("B");
  });

  it("includes weak areas", () => {
    const scores = [mkScore(1, "Weak"), mkScore(4, "Strong")];
    const out = generateFindings(scores);
    expect(out).toContain("Weak");
    expect(out).toContain("Strong");
  });
});

describe("generateRecommendations", () => {
  const mkDim = (name: string) => ({ name });

  it("returns generic when no weak scores", () => {
    const scores = [{ score: 4, dimension: mkDim("A") }];
    expect(generateRecommendations(scores)).toContain("pratiques");
  });

  it("mentions weak dimensions", () => {
    const scores = [{ score: 1, dimension: mkDim("Gap") }];
    expect(generateRecommendations(scores)).toContain("Gap");
  });
});

describe("evaluationToCsvRows", () => {
  it("produces header + one row per dimension", () => {
    const evalData = {
      candidateName: "Alice",
      candidateRole: "Engineer",
      evaluatorName: "Bob",
      evaluationDate: new Date("2025-02-15"),
      status: "draft",
      dimensionScores: [
        {
          dimension: { name: "Data Quality" },
          score: 3,
          justification: "Good",
          examplesObserved: "x",
          confidence: "high",
        },
      ],
    } as unknown as Parameters<typeof evaluationToCsvRows>[0];

    const rows = evaluationToCsvRows(evalData);
    expect(rows[0]).toContain("candidateName");
    expect(rows.length).toBe(2);
    expect(rows[1]).toContain("Alice");
    expect(rows[1]).toContain("Data Quality");
    expect(rows[1]).toContain("3");
  });
});
