import { describe, expect, it } from "vitest";
import { diagnosticQuestions } from "@/content/diagnostic";
import {
  auditHrefForResult,
  isDiagnosticComplete,
  scoreDiagnostic,
  type DiagnosticAnswers,
} from "@/lib/diagnostic";

const saasMeasurement: Required<DiagnosticAnswers> = {
  model: "saas",
  objective: "efficiency",
  spend: "50k-150k",
  constraint: "tracking",
  platform: "linkedin",
};

describe("isDiagnosticComplete", () => {
  it("is false until all five questions have an answer", () => {
    expect(isDiagnosticComplete({})).toBe(false);
    expect(isDiagnosticComplete({ model: "saas", objective: "scale" })).toBe(false);
    expect(isDiagnosticComplete(saasMeasurement)).toBe(true);
  });

  it("rejects values that are not offered options", () => {
    expect(isDiagnosticComplete({ ...saasMeasurement, spend: "a-lot" })).toBe(false);
  });
});

describe("scoreDiagnostic", () => {
  it("names measurement + conversion for a SaaS team constrained by tracking", () => {
    const result = scoreDiagnostic(saasMeasurement);
    expect(result.primary).toBe("measurement");
    expect(result.secondary).toBe("conversion");
    expect(result.title).toBe("Measurement + conversion infrastructure");
  });

  it("breaks ties in favour of the stated objective", () => {
    const result = scoreDiagnostic({
      model: "ecommerce",
      objective: "scale",
      spend: "10k-50k",
      constraint: "creative",
      platform: "meta",
    });
    expect(result.primary).toBe("creative");
    expect(result.secondary).toBe("demand");
    expect(result.title).toBe("Demand capture + creative testing");
  });

  it("puts the stated constraint first when scores tie", () => {
    const result = scoreDiagnostic({
      model: "ecommerce",
      objective: "efficiency",
      spend: "150k-plus",
      constraint: "retention",
      platform: "google",
    });
    expect(result.scores.retention).toBe(result.scores.measurement);
    expect(result.primary).toBe("retention");
    expect(result.secondary).toBe("measurement");
  });

  it("returns three model-specific checks and never a numeric projection", () => {
    const result = scoreDiagnostic(saasMeasurement);
    expect(result.checks).toHaveLength(3);
    const serialized = JSON.stringify(result.checks) + result.summary;
    expect(serialized).not.toMatch(/\$\s?\d|\d+\s?%|revenue increase|guarantee/i);
  });

  it("produces a named result for every possible combination of answers", () => {
    const [model, objective, spend, constraint, platform] = diagnosticQuestions;
    for (const m of model.options)
      for (const o of objective.options)
        for (const s of spend.options)
          for (const c of constraint.options)
            for (const p of platform.options) {
              const result = scoreDiagnostic({
                model: m.value,
                objective: o.value,
                spend: s.value,
                constraint: c.value,
                platform: p.value,
              });
              expect(result.title.length).toBeGreaterThan(0);
              expect(result.primary).not.toBe(result.secondary);
            }
  });

  it("throws when answers are incomplete", () => {
    expect(() => scoreDiagnostic({ model: "saas" })).toThrow();
  });
});

describe("auditHrefForResult", () => {
  it("prefills the audit form with the model and focus areas", () => {
    const href = auditHrefForResult(scoreDiagnostic(saasMeasurement));
    expect(href).toBe("/growth-audit?model=saas&focus=measurement%2Cconversion");
  });
});
