import { describe, expect, it } from "vitest";
import {
  buildStrategicRecommendations,
  computeDimensionDisplayScore,
  getGapLabel,
  getGapLevel,
  getMaturityLevel,
  getNextMaturityTarget,
} from "@/lib/talleres/metrics";

describe("talleres metrics", () => {
  it("resolves maturity levels with expected thresholds", () => {
    expect(getMaturityLevel(22).label).toBe("Inicial");
    expect(getMaturityLevel(45).label).toBe("En Desarrollo");
    expect(getMaturityLevel(61).label).toBe("Preparado");
    expect(getMaturityLevel(92).label).toBe("Avanzado");
  });

  it("computes next level targets", () => {
    expect(getNextMaturityTarget(39)).toEqual({
      label: "En Desarrollo",
      threshold: 40,
      pointsNeeded: 1,
      progressToTarget: 95,
    });

    expect(getNextMaturityTarget(66)).toEqual({
      label: "Avanzado",
      threshold: 80,
      pointsNeeded: 14,
      progressToTarget: 30,
    });

    expect(getNextMaturityTarget(85)).toBeNull();
  });

  it("computes gap level labels", () => {
    expect(getGapLevel(84)).toBe("baja");
    expect(getGapLabel("baja")).toBe("Brecha Baja");
    expect(getGapLevel(75)).toBe("media");
    expect(getGapLabel("media")).toBe("Brecha Media");
    expect(getGapLevel(41)).toBe("alta");
    expect(getGapLabel("alta")).toBe("Brecha Alta");
  });

  it("applies workshop bonus to display score with cap", () => {
    expect(computeDimensionDisplayScore(62, 1)).toBe(70);
    expect(computeDimensionDisplayScore(62, 2)).toBe(78);
    expect(computeDimensionDisplayScore(98, 2)).toBe(100);
  });

  it("prioritizes recommendations by severity and score", () => {
    const result = buildStrategicRecommendations([
      { moduleKey: "cultura-organizacional", moduleName: "Cultura Organizacional", displayScore: 81, gapLevel: "baja" },
      { moduleKey: "impacto-social-equidad", moduleName: "Impacto Social y Equidad", displayScore: 40, gapLevel: "alta" },
      { moduleKey: "innovacion-sostenibilidad", moduleName: "Innovacion y Sostenibilidad", displayScore: 62, gapLevel: "media" },
      { moduleKey: "liderazgo-vision-estrategica", moduleName: "Liderazgo y Vision Estrategica", displayScore: 57, gapLevel: "alta" },
    ]);

    expect(result).toHaveLength(3);
    expect(result[0]?.moduleKey).toBe("impacto-social-equidad");
    expect(result[1]?.moduleKey).toBe("liderazgo-vision-estrategica");
    expect(result[2]?.moduleKey).toBe("innovacion-sostenibilidad");
  });
});
