import { describe, expect, it } from "vitest";
import { OverallScoreMethod } from "@prisma/client";
import { computeAssessmentSnapshot } from "@/lib/scoring-core";

describe("computeAssessmentSnapshot", () => {
  it("computes module and overall scores from answered questions", () => {
    const modules = [
      {
        moduleId: "m1",
        moduleKey: "liderazgo",
        moduleName: "Liderazgo",
        questionWeights: [
          { questionId: "q1", maxWeight: 5 },
          { questionId: "q2", maxWeight: 5 },
        ],
      },
      {
        moduleId: "m2",
        moduleKey: "datos",
        moduleName: "Datos",
        questionWeights: [
          { questionId: "q3", maxWeight: 5 },
          { questionId: "q4", maxWeight: 5 },
        ],
      },
    ];

    const answers = new Map<string, number>([
      ["q1", 5],
      ["q2", 3],
      ["q3", 1],
    ]);

    const snapshot = computeAssessmentSnapshot(modules, answers, {
      overallScoreMethod: OverallScoreMethod.EQUAL_ALL_MODULES,
      moduleWeights: {},
    });

    expect(snapshot.moduleScores).toHaveLength(2);
    expect(snapshot.moduleScores[0]?.score).toBe(80);
    expect(snapshot.moduleScores[0]?.completion).toBe(100);
    expect(snapshot.moduleScores[0]?.status).toBe("Completado");

    expect(snapshot.moduleScores[1]?.score).toBe(20);
    expect(snapshot.moduleScores[1]?.completion).toBe(50);
    expect(snapshot.moduleScores[1]?.status).toBe("En curso");

    expect(snapshot.overallScore).toBe(50);
    expect(snapshot.answeredQuestions).toBe(3);
    expect(snapshot.totalQuestions).toBe(4);
    expect(snapshot.strongestModule?.moduleKey).toBe("liderazgo");
    expect(snapshot.weakestModule?.moduleKey).toBe("datos");
  });

  it("returns zero snapshot for empty modules", () => {
    const snapshot = computeAssessmentSnapshot([], new Map<string, number>(), {
      overallScoreMethod: OverallScoreMethod.EQUAL_ALL_MODULES,
      moduleWeights: {},
    });

    expect(snapshot.overallScore).toBe(0);
    expect(snapshot.moduleScores).toHaveLength(0);
    expect(snapshot.strongestModule).toBeNull();
    expect(snapshot.weakestModule).toBeNull();
  });
});
