import { describe, expect, it } from "vitest";
import { toReportingScenario } from "./reportingScenarios";

describe("toReportingScenario", () => {
  const baseRow = {
    id: "12",
    name: "EPC C by 2030",
    goal: "Increasing EPC",
    goal_value: "C",
    budget: 15000,
    housing_type: "Social",
    created_at: "2026-07-04T09:10:00.000Z",
  };

  it("normalises a string timestamp from raw db.execute to an ISO string", () => {
    // Regression: raw db.execute returns created_at as a string, so calling
    // .toISOString() on it threw. The mapper must accept a string.
    const s = toReportingScenario(baseRow, new Set(["12"]));
    expect(s.createdAt).toBe("2026-07-04T09:10:00.000Z");
  });

  it("also accepts a Date timestamp", () => {
    const s = toReportingScenario(
      { ...baseRow, created_at: new Date("2026-07-04T09:10:00.000Z") },
      new Set(),
    );
    expect(s.createdAt).toBe("2026-07-04T09:10:00.000Z");
  });

  it("marks a scenario modelled only when its id is in the modelled set", () => {
    expect(toReportingScenario(baseRow, new Set(["12"])).status).toBe(
      "modelled",
    );
    expect(toReportingScenario(baseRow, new Set(["99"])).status).toBe(
      "awaiting_modelling",
    );
  });

  it("falls back to a placeholder name and coerces a bigint id", () => {
    const s = toReportingScenario(
      { ...baseRow, id: 12n as unknown as bigint, name: null },
      new Set(["12"]),
    );
    expect(s.id).toBe(12);
    expect(s.name).toBe("Scenario 12");
    expect(s.status).toBe("modelled");
  });
});
