import { describe, it, expect, vi, beforeEach, afterEach } from "vitest";

vi.mock("@repo/core-cms", () => ({ default: Promise.resolve({}) }));
vi.mock("payload", () => ({
  getPayload: vi.fn(async () => ({ jobs: { queue: vi.fn() } })),
}));
vi.mock("@repo/blog/di/bind-production", () => ({
  bindProductionBlog: vi.fn(),
}));
vi.mock("@repo/auth/di/bind-production", () => ({
  bindProductionAuth: vi.fn(),
}));
vi.mock("@repo/marketing-pages/di/bind-production", () => ({
  bindProductionMarketingPages: vi.fn(),
}));
vi.mock("@repo/navigation/di/bind-production", () => ({
  bindProductionNavigation: vi.fn(),
}));
vi.mock("@repo/media/di/bind-production", () => ({
  bindProductionMedia: vi.fn(),
}));
vi.mock("@repo/blog/di/bind-dev-seed", () => ({ bindDevSeedBlog: vi.fn() }));
vi.mock("@repo/auth/di/bind-dev-seed", () => ({ bindDevSeedAuth: vi.fn() }));
vi.mock("@repo/marketing-pages/di/bind-dev-seed", () => ({
  bindDevSeedMarketingPages: vi.fn(),
}));
vi.mock("@repo/navigation/di/bind-dev-seed", () => ({
  bindDevSeedNavigation: vi.fn(),
}));
vi.mock("@repo/media/di/bind-dev-seed", () => ({ bindDevSeedMedia: vi.fn() }));
vi.mock("@repo/core-shared/instrumentation", async (importOriginal) => {
  const actual =
    await importOriginal<typeof import("@repo/core-shared/instrumentation")>();
  const mockedOtel = vi.fn(actual.bindOtelInstrumentation);
  return {
    ...actual,
    bindOtelInstrumentation: mockedOtel,
    // Deprecated alias — points to same spy so existing assertions still work.
    bindSentryInstrumentation: mockedOtel,
    bindNoopInstrumentation: vi.fn(actual.bindNoopInstrumentation),
  };
});

describe("bindAllProduction", () => {
  beforeEach(() => {
    vi.resetModules();
    vi.clearAllMocks();
  });

  it("binds all five feature production repos", async () => {
    const { bindAllProduction } = await import("./bind-production");
    const { bindProductionBlog } =
      await import("@repo/blog/di/bind-production");
    const { bindProductionAuth } =
      await import("@repo/auth/di/bind-production");
    const { bindProductionMarketingPages } =
      await import("@repo/marketing-pages/di/bind-production");
    const { bindProductionNavigation } =
      await import("@repo/navigation/di/bind-production");
    const { bindProductionMedia } =
      await import("@repo/media/di/bind-production");

    await bindAllProduction();

    expect(bindProductionBlog).toHaveBeenCalledOnce();
    expect(bindProductionAuth).toHaveBeenCalledOnce();
    expect(bindProductionMarketingPages).toHaveBeenCalledOnce();
    expect(bindProductionNavigation).toHaveBeenCalledOnce();
    expect(bindProductionMedia).toHaveBeenCalledOnce();
  });

  it("is idempotent — second call does not re-bind", async () => {
    const { bindAllProduction } = await import("./bind-production");
    const { bindProductionBlog } =
      await import("@repo/blog/di/bind-production");
    await bindAllProduction();
    await bindAllProduction();
    expect(bindProductionBlog).toHaveBeenCalledOnce();
  });

  it("passes a Payload-backed queue to each per-feature binder", async () => {
    const { bindAllProduction } = await import("./bind-production");
    const { bindProductionAuth } =
      await import("@repo/auth/di/bind-production");
    const { PayloadJobQueue } = await import("@repo/core-shared/jobs");

    await bindAllProduction();

    const ctx = vi.mocked(bindProductionAuth).mock.calls[0]![0];
    expect(ctx.bus).toBeUndefined();
    expect(ctx.queue).toBeInstanceOf(PayloadJobQueue);
  });
});

describe("bindAllDevSeed", () => {
  beforeEach(() => {
    vi.resetModules();
    vi.clearAllMocks();
  });

  it("passes an in-memory queue (no bus) to each per-feature dev-seed binder", async () => {
    const { bindAllDevSeed } = await import("./bind-production");
    const { bindDevSeedAuth } = await import("@repo/auth/di/bind-dev-seed");
    const { InMemoryJobQueue } = await import("@repo/core-shared/jobs");

    await bindAllDevSeed();

    const ctx = vi.mocked(bindDevSeedAuth).mock.calls[0]![0];
    expect(ctx.bus).toBeUndefined();
    expect(ctx.queue).toBeInstanceOf(InMemoryJobQueue);
  });
});

describe("bindAll dispatcher", () => {
  beforeEach(() => {
    vi.resetModules();
    vi.clearAllMocks();
    vi.unstubAllEnvs();
  });

  afterEach(() => {
    vi.unstubAllEnvs();
  });

  it("USE_DEV_SEED='true' wins → dispatches to bindAllDevSeed", async () => {
    vi.stubEnv("USE_DEV_SEED", "true");
    vi.stubEnv("NODE_ENV", "production"); // even in production, dev seed wins
    const { bindAll } = await import("./bind-production");
    const { bindDevSeedBlog } = await import("@repo/blog/di/bind-dev-seed");
    const { bindProductionBlog } =
      await import("@repo/blog/di/bind-production");

    await bindAll();

    expect(bindDevSeedBlog).toHaveBeenCalledOnce();
    expect(bindProductionBlog).not.toHaveBeenCalled();
  });

  it("NODE_ENV='production' (no override) → dispatches to bindAllProduction", async () => {
    vi.stubEnv("NODE_ENV", "production");
    const { bindAll } = await import("./bind-production");
    const { bindProductionBlog } =
      await import("@repo/blog/di/bind-production");
    const { bindDevSeedBlog } = await import("@repo/blog/di/bind-dev-seed");

    await bindAll();

    expect(bindProductionBlog).toHaveBeenCalledOnce();
    expect(bindDevSeedBlog).not.toHaveBeenCalled();
  });

  it("NODE_ENV='development' → dispatches to bindAllDevSeed (developer default)", async () => {
    vi.stubEnv("NODE_ENV", "development");
    const { bindAll } = await import("./bind-production");
    const { bindDevSeedBlog } = await import("@repo/blog/di/bind-dev-seed");
    const { bindProductionBlog } =
      await import("@repo/blog/di/bind-production");

    await bindAll();

    expect(bindDevSeedBlog).toHaveBeenCalledOnce();
    expect(bindProductionBlog).not.toHaveBeenCalled();
  });

  it("USE_DEV_SEED='false' is treated as not-set (only 'true' triggers dev seed)", async () => {
    vi.stubEnv("USE_DEV_SEED", "false");
    vi.stubEnv("NODE_ENV", "production");
    const { bindAll } = await import("./bind-production");
    const { bindProductionBlog } =
      await import("@repo/blog/di/bind-production");
    const { bindDevSeedBlog } = await import("@repo/blog/di/bind-dev-seed");

    await bindAll();

    expect(bindProductionBlog).toHaveBeenCalledOnce();
    expect(bindDevSeedBlog).not.toHaveBeenCalled();
  });
});

describe("bindAll instrumentation orthogonality", () => {
  beforeEach(() => {
    vi.resetModules();
    vi.clearAllMocks();
    vi.unstubAllEnvs();
  });

  afterEach(() => {
    vi.unstubAllEnvs();
  });

  // In the mock setup above, bindSentryInstrumentation is an alias that points
  // to the same spy as bindOtelInstrumentation. Assertions against either name
  // verify the same call, which also validates the deprecation alias is wired.

  it("DSN absent → bindNoopInstrumentation regardless of NODE_ENV", async () => {
    vi.stubEnv("WEB_NEXT_SENTRY_DSN", "");
    vi.stubEnv("NODE_ENV", "production");
    const { bindAll } = await import("./bind-production");
    const { bindNoopInstrumentation, bindOtelInstrumentation } =
      await import("@repo/core-shared/instrumentation");

    await bindAll();

    expect(bindNoopInstrumentation).toHaveBeenCalledOnce();
    expect(bindOtelInstrumentation).not.toHaveBeenCalled();
  });

  it("DSN set → bindOtelInstrumentation regardless of NODE_ENV", async () => {
    vi.stubEnv("WEB_NEXT_SENTRY_DSN", "https://x@y/1");
    vi.stubEnv("NODE_ENV", "development");
    const { bindAll } = await import("./bind-production");
    const { bindNoopInstrumentation, bindOtelInstrumentation } =
      await import("@repo/core-shared/instrumentation");

    await bindAll();

    expect(bindOtelInstrumentation).toHaveBeenCalledOnce();
    expect(bindNoopInstrumentation).not.toHaveBeenCalled();
  });

  it("OTel instrumentation works alongside dev seed (USE_DEV_SEED=true)", async () => {
    vi.stubEnv("USE_DEV_SEED", "true");
    vi.stubEnv("WEB_NEXT_SENTRY_DSN", "https://x@y/1");
    const { bindAll } = await import("./bind-production");
    const { bindOtelInstrumentation } =
      await import("@repo/core-shared/instrumentation");
    const { bindDevSeedBlog } = await import("@repo/blog/di/bind-dev-seed");

    await bindAll();

    expect(bindOtelInstrumentation).toHaveBeenCalledOnce();
    expect(bindDevSeedBlog).toHaveBeenCalledOnce();
  });

  it("Noop instrumentation works alongside production binding (DSN unset, NODE_ENV=production)", async () => {
    vi.stubEnv("WEB_NEXT_SENTRY_DSN", "");
    vi.stubEnv("NODE_ENV", "production");
    const { bindAll } = await import("./bind-production");
    const { bindNoopInstrumentation } =
      await import("@repo/core-shared/instrumentation");
    const { bindProductionBlog } =
      await import("@repo/blog/di/bind-production");

    await bindAll();

    expect(bindNoopInstrumentation).toHaveBeenCalledOnce();
    expect(bindProductionBlog).toHaveBeenCalledOnce();
  });
});
