// Cross-feature proof-of-life: signing up in @repo/auth publishes
// userSignedUpEvent on the shared bus, marketing-pages' subscriber enqueues
// a send-welcome-email job on the shared queue, and dev-seed's
// InMemoryJobQueue.register() dispatches it to the wrapped job — which
// records the call on the bound RecordingMailerService.

import "reflect-metadata";
import { describe, it, expect, beforeEach } from "vitest";
import { bindAllDevSeed, __resetBindStateForTests } from "@/server/bind-production";
import { authContainer } from "@repo/auth/di/container";
import { AUTH_SYMBOLS } from "@repo/auth/di/symbols";
import type { ISignUpController } from "@repo/auth";
import { marketingPagesContainer } from "@repo/marketing-pages/di/container";
import { MARKETING_PAGES_SYMBOLS } from "@repo/marketing-pages/di/symbols";
import { RecordingMailerService } from "@repo/marketing-pages/services/recording-mailer";

describe("e2e: sign-up triggers welcome email via cross-feature event", () => {
  beforeEach(() => {
    __resetBindStateForTests();
  });

  it("delivers a welcome email after a successful sign-up", async () => {
    await bindAllDevSeed();

    const mailer = marketingPagesContainer.get<RecordingMailerService>(
      MARKETING_PAGES_SYMBOLS.IMailerService,
    );
    const signUp = authContainer.get<ISignUpController>(AUTH_SYMBOLS.ISignUpController);

    await signUp({
      username: "testuser",
      password: "secret_password",
      confirmPassword: "secret_password",
    });

    // The InMemoryJobQueue dispatches the registered handler on setImmediate;
    // wait for microtasks + one setImmediate tick to settle.
    await new Promise((r) => setImmediate(r));

    expect(mailer.sent).toEqual([
      { userId: expect.any(String), email: "testuser@example.local" },
    ]);
  });
});
