import { describe, expect, it } from "vitest";
import type { NormativeAnalysisResult, NormativeDocumentType } from "@/lib/normative-analysis/analyze";
import { mergeNormativeBundleSources } from "@/lib/normative-analysis/bundle";

function makeResult(label: string, documentType: NormativeDocumentType): NormativeAnalysisResult {
  return {
    executiveSummary: `Resumen ${label}`,
    participationViability: {
      score: 70,
      level: "media",
      rationale: "Racional",
    },
    risk: {
      level: "medio",
      description: "Riesgo controlable",
    },
    requiredChecklist: [
      {
        title: "Formato de personalidad",
        description: `Checklist ${label}`,
      },
    ],
    criticalRequirements: [
      {
        title: "Carta critica",
        description: `Critico ${label}`,
        severity: "alto",
      },
    ],
    lockIns: [
      {
        title: "Candado propietario",
        impact: `Impacto ${label}`,
        recommendation: "Solicitar equivalencia",
      },
    ],
    preparationRecommendations: [
      {
        title: `Preparacion ${label}`,
        description: "Accion",
        priority: "alta",
      },
    ],
    officialFormats: ["Formato A"],
    valueForMoney: {
      score: 65,
      signals: [`Senal ${label}`],
      actions: ["Analizar costo total"],
    },
    circularEconomy: {
      applicable: documentType === "ANEXOS",
      signals: ["Reciclaje"],
      actions: ["Plan de residuos"],
    },
    sourceTraceability: [
      {
        documentType,
        sourceLabel: label,
        fields: ["requiredChecklist"],
      },
    ],
    confidence: "medium",
  };
}

describe("mergeNormativeBundleSources", () => {
  it("prioritizes Bases over Convocatoria on conflicting checklist items", () => {
    const convocatoria = makeResult("convocatoria", "CONVOCATORIA");
    convocatoria.requiredChecklist[0] = {
      title: "Formato de personalidad",
      description: "Texto convocatoria",
    };

    const bases = makeResult("bases", "BASES_LICITACION");
    bases.requiredChecklist[0] = {
      title: "Formato de personalidad",
      description: "Texto bases",
    };

    const merged = mergeNormativeBundleSources([
      {
        sourceId: "conv",
        fileName: "convocatoria.pdf",
        documentType: "CONVOCATORIA",
        result: convocatoria,
      },
      {
        sourceId: "bases",
        fileName: "bases.pdf",
        documentType: "BASES_LICITACION",
        result: bases,
      },
    ]);

    expect(merged.result.requiredChecklist[0]?.description).toBe("Texto bases");
    expect(merged.conflicts.length).toBeGreaterThan(0);
  });

  it("aggregates traceability and value-for-money signals", () => {
    const merged = mergeNormativeBundleSources([
      {
        sourceId: "bases",
        fileName: "bases.pdf",
        documentType: "BASES_LICITACION",
        result: makeResult("bases", "BASES_LICITACION"),
      },
      {
        sourceId: "anexo",
        fileName: "anexo.pdf",
        documentType: "ANEXOS",
        result: makeResult("anexo", "ANEXOS"),
      },
    ]);

    expect(merged.result.sourceTraceability.length).toBe(2);
    expect(merged.result.valueForMoney.signals.length).toBeGreaterThanOrEqual(2);
    expect(merged.result.circularEconomy.actions.length).toBeGreaterThan(0);
  });
});
