import { describe, expect, it } from "vitest";
import { toRouteHintSummary, toRouterInput } from "../src/services/routeHintAdapter";
import { normalizedFixture, normalizedFixtureV2, normalizedFixtureV2_0_1, normalizedFixtureV2_0_2 } from "./fixtures";

describe("routeHintAdapter", () => {
  it("builds v1 route hint summary", () => {
    const summary = toRouteHintSummary(normalizedFixture());
    expect(summary.mode).toBe("legacy_v1");
    if (summary.mode !== "legacy_v1") {
      throw new Error("Expected legacy_v1 summary");
    }
    expect(summary.route_hint).toBe("hybrid_store_plus_live");
    expect(summary.decision_flags.needs_cross_entity_join).toBe(true);
    expect(summary.entities.accounts_mentioned).toEqual(["60"]);
  });

  it("builds v2 deterministic route simulation", () => {
    const summary = toRouteHintSummary(normalizedFixtureV2());
    expect(summary.mode).toBe("deterministic_v2");
    if (summary.mode !== "deterministic_v2") {
      throw new Error("Expected deterministic_v2 summary");
    }
    expect(summary.planner.total_fragments).toBe(1);
    expect(summary.decisions[0]?.route).toBe("hybrid_store_plus_live");
  });

  it("builds router input contract for v1", () => {
    const routerInput = toRouterInput(normalizedFixture());
    expect(routerInput.route_hint).toBe("hybrid_store_plus_live");
    expect(routerInput.intent_class).toBe("cross_entity");
  });

  it("keeps v2.0.1 soft assumptions executable in deterministic routing", () => {
    const summary = toRouteHintSummary(normalizedFixtureV2_0_1());
    expect(summary.mode).toBe("deterministic_v2");
    if (summary.mode !== "deterministic_v2") {
      throw new Error("Expected deterministic_v2 summary");
    }
    expect(summary.fallback.type).toBe("none");
    expect(summary.decisions[0]?.execution_readiness).toBe("executable_with_soft_assumptions");
    expect(summary.decisions[0]?.route).toBe("store_feature_risk");
  });

  it("uses explicit v2.0.2 route_status/no_route_reason contract", () => {
    const summary = toRouteHintSummary(normalizedFixtureV2_0_2());
    expect(summary.mode).toBe("deterministic_v2");
    if (summary.mode !== "deterministic_v2") {
      throw new Error("Expected deterministic_v2 summary");
    }
    expect(summary.decisions[0]?.route_status).toBe("routed");
    expect(summary.decisions[0]?.no_route_reason).toBeNull();
    expect(summary.decisions[0]?.route).toBe("store_feature_risk");

    const routerInput = toRouterInput(normalizedFixtureV2_0_2());
    const first = (routerInput.fragments as Array<Record<string, unknown>>)[0];
    expect(first.route_status).toBe("routed");
    expect(first.no_route_reason).toBeNull();
  });
});
