/**
 * Targeted runtime smoke for the authenticated conversation ingress.
 *
 * Simulates the exact sequence the Flue route middleware drives, end to end,
 * against the real Convex schema and modules via convexTest:
 *
 *   1. ensurePersonalOrganization  (bearer auth → org resolution)
 *   2. beginUserMessage            (evidence before Flue admission)
 *   3. markAdmitted                (Flue receipt submission id)
 *
 * Then verifies the stored normalized evidence has exact raw text, organization
 * scope, conversation/message identity, request id, role user, and the admitted
 * submission id.
 *
 * This is a smoke, not a unit test: delete after the live browser smoke is
 * confirmed.
 */
import { convexTest } from "convex-test";
import { anyApi } from "convex/server";
import { describe, expect, test } from "vitest";

import schema from "./schema";

declare global {
  interface ImportMeta {
    readonly glob: (pattern: string) => Record<string, () => Promise<unknown>>;
  }
}

const modules = import.meta.glob("./**/*.ts");
const api = anyApi;

const ID_A = "https://convex.test|smoke-user";
const identityA = { tokenIdentifier: ID_A };

const newTest = () => convexTest({ schema, modules });

describe("conversation ingress smoke", () => {
  test("one user message produces normalized admitted evidence end to end", async () => {
    const t = newTest();

    // 1. Auth + org resolution (the middleware resolves this from the bearer
    //    token via a request-scoped ConvexHttpClient).
    const org = await t
      .withIdentity(identityA)
      .mutation(api.organizations.ensurePersonalOrganization, {});

    const rawText = "  Fix the login bug  \n\t  please  ";
    const clientRequestId = "smoke-req-001";

    // 2. Begin evidence before Flue admission.
    const begun = await t
      .withIdentity(identityA)
      .mutation(api.conversationMessages.beginUserMessage, {
        clientRequestId,
        organizationId: org._id,
        rawText,
      });
    expect(begun.status).toBe("admitting");
    expect(begun.rawText).toBe(rawText);

    // 3. Mark admitted with the Flue receipt submission id.
    const submissionId = "flue-sub-abc123";
    await t
      .withIdentity(identityA)
      .mutation(api.conversationMessages.markAdmitted, {
        clientRequestId,
        organizationId: org._id,
        submissionId,
      });

    // 4. Verify normalized evidence via authorized observation.
    const observed = await t
      .withIdentity(identityA)
      .query(api.conversationMessages.getByRequest, {
        clientRequestId,
        organizationId: org._id,
      });

    expect(observed).not.toBeNull();
    expect(observed?.rawText).toBe(rawText);
    expect(observed?.organizationId).toBe(org._id);
    expect(observed?.conversationId).toBe(org._id);
    expect(typeof observed?.messageId).toBe("string");
    expect(observed?.clientRequestId).toBe(clientRequestId);
    expect(observed?.role).toBe("user");
    expect(observed?.status).toBe("admitted");
    expect(observed?.submissionId).toBe(submissionId);
  });
});
