import {
  withSpan,
  withCapture,
  INSTRUMENTATION_SYMBOLS,
  type ITracer,
  type ILogger,
} from "@repo/core-shared/instrumentation";
import type { BindContext } from "@repo/core-shared/di";
import { assertFeatureConformance } from "@repo/core-shared/conformance";
import { marketingPagesManifest } from "../feature.manifest.js";
import { marketingPagesContainer } from "./container.js";
import { MARKETING_PAGES_SYMBOLS } from "./symbols.js";
import { MockPagesRepository } from "../infrastructure/repositories/pages.repository.mock.js";
import { buildDevPages, buildDevSiteSettings } from "../__seeds__/dev.js";
import { getSiteSettingsUseCase } from "../application/use-cases/get-site-settings.use-case.js";
import { getPageBySlugUseCase } from "../application/use-cases/get-page-by-slug.use-case.js";
import { getSiteSettingsController } from "../interface-adapters/controllers/get-site-settings.controller.js";
import { getPageBySlugController } from "../interface-adapters/controllers/get-page-by-slug.controller.js";
import type { IPagesRepository } from "../application/repositories/pages.repository.interface.js";
import type { ISiteSettingsRepository } from "../application/repositories/site-settings.repository.interface.js";
import { userSignedUpEvent } from "@repo/auth";
import { onAuthUserSignedUpHandler } from "../events/handlers/on-auth-user-signed-up.handler.js";
import { sendWelcomeEmailJob, type ISendWelcomeEmailJob, type SendWelcomeEmailInput } from "../jobs/send-welcome-email.job.js";
import { RecordingMailerService } from "../infrastructure/services/recording-mailer.service.js";
import type { IMailerService } from "../application/services/mailer.service.interface.js";

/**
 * Replace the default empty mocks with populated ones for dev mode + storybook.
 *
 * Call this from app boot when `USE_DEV_SEED=true`, mutually exclusive with
 * `bindProductionMarketingPages(config)`. Tests must NOT call this — they
 * construct mocks directly and seed via factories per-test.
 *
 * Idempotent: safe to call multiple times; each call rebuilds fresh populated
 * repos and rebinds both symbols.
 */
export async function bindDevSeedMarketingPages(ctx: BindContext): Promise<void> {
  const { tracer, logger, bus, queue, realtime, realtimeRegistry } = ctx;

  // Bind shared instrumentation into feature container
  if (marketingPagesContainer.isBound(INSTRUMENTATION_SYMBOLS.TRACER)) {
    marketingPagesContainer.unbind(INSTRUMENTATION_SYMBOLS.TRACER);
  }
  if (marketingPagesContainer.isBound(INSTRUMENTATION_SYMBOLS.LOGGER)) {
    marketingPagesContainer.unbind(INSTRUMENTATION_SYMBOLS.LOGGER);
  }
  marketingPagesContainer.bind<ITracer>(INSTRUMENTATION_SYMBOLS.TRACER).toConstantValue(tracer);
  marketingPagesContainer.bind<ILogger>(INSTRUMENTATION_SYMBOLS.LOGGER).toConstantValue(logger);

  // Pages repository — MockPagesRepository accepts an initial array.
  if (marketingPagesContainer.isBound(MARKETING_PAGES_SYMBOLS.IPagesRepository)) {
    marketingPagesContainer.unbind(MARKETING_PAGES_SYMBOLS.IPagesRepository);
  }
  const pagesRepo = new MockPagesRepository(buildDevPages(), tracer, logger);
  marketingPagesContainer
    .bind<IPagesRepository>(MARKETING_PAGES_SYMBOLS.IPagesRepository)
    .toConstantValue(pagesRepo);

  // Site settings repository
  if (marketingPagesContainer.isBound(MARKETING_PAGES_SYMBOLS.ISiteSettingsRepository)) {
    marketingPagesContainer.unbind(MARKETING_PAGES_SYMBOLS.ISiteSettingsRepository);
  }
  const settings = buildDevSiteSettings();
  // Inline constant so dev seed returns exactly the seeded value (not the mock's default)
  const siteSettingsRepo: ISiteSettingsRepository = {
    getSiteSettings: async () => settings,
  };
  marketingPagesContainer
    .bind<ISiteSettingsRepository>(MARKETING_PAGES_SYMBOLS.ISiteSettingsRepository)
    .toConstantValue(siteSettingsRepo);

  // Wrap use cases + controllers identically to bind-production
  const wrappedGetSiteSettings = withSpan(
    tracer,
    { name: "marketing-pages.getSiteSettings", op: "use-case" },
    withCapture(
      logger,
      { feature: "marketing-pages", layer: "use-case", name: "marketing-pages.getSiteSettings" },
      getSiteSettingsUseCase(siteSettingsRepo),
    ),
  );
  const wrappedGetPageBySlug = withSpan(
    tracer,
    { name: "marketing-pages.getPageBySlug", op: "use-case" },
    withCapture(
      logger,
      { feature: "marketing-pages", layer: "use-case", name: "marketing-pages.getPageBySlug" },
      getPageBySlugUseCase(pagesRepo),
    ),
  );

  for (const sym of [
    MARKETING_PAGES_SYMBOLS.IGetSiteSettingsUseCase,
    MARKETING_PAGES_SYMBOLS.IGetPageBySlugUseCase,
    MARKETING_PAGES_SYMBOLS.IGetSiteSettingsController,
    MARKETING_PAGES_SYMBOLS.IGetPageBySlugController,
  ]) {
    if (marketingPagesContainer.isBound(sym)) marketingPagesContainer.unbind(sym);
  }
  marketingPagesContainer
    .bind(MARKETING_PAGES_SYMBOLS.IGetSiteSettingsUseCase)
    .toConstantValue(wrappedGetSiteSettings);
  marketingPagesContainer
    .bind(MARKETING_PAGES_SYMBOLS.IGetPageBySlugUseCase)
    .toConstantValue(wrappedGetPageBySlug);

  marketingPagesContainer
    .bind(MARKETING_PAGES_SYMBOLS.IGetSiteSettingsController)
    .toConstantValue(
      withSpan(
        tracer,
        { name: "marketing-pages.getSiteSettings", op: "controller" },
        withCapture(
          logger,
          { feature: "marketing-pages", layer: "controller", name: "marketing-pages.getSiteSettings" },
          getSiteSettingsController(wrappedGetSiteSettings),
        ),
      ),
    );
  marketingPagesContainer
    .bind(MARKETING_PAGES_SYMBOLS.IGetPageBySlugController)
    .toConstantValue(
      withSpan(
        tracer,
        { name: "marketing-pages.getPageBySlug", op: "controller" },
        withCapture(
          logger,
          { feature: "marketing-pages", layer: "controller", name: "marketing-pages.getPageBySlug" },
          getPageBySlugController(wrappedGetPageBySlug),
        ),
      ),
    );
  // Bind the dev-seed mailer (recording so e2e tests can inspect calls).
  if (marketingPagesContainer.isBound(MARKETING_PAGES_SYMBOLS.IMailerService)) {
    marketingPagesContainer.unbind(MARKETING_PAGES_SYMBOLS.IMailerService);
  }
  const mailer = new RecordingMailerService();
  marketingPagesContainer
    .bind<IMailerService>(MARKETING_PAGES_SYMBOLS.IMailerService)
    .toConstantValue(mailer);

  // <gen:event-handlers>
  // onAuthUserSignedUpHandler subscription — generated, edit the handler file (not this block) for behavior.
  // bus is optional: when @repo/core-events is not scaffolded, ctx.bus is undefined and the handler is not bound.
  // queue (IJobQueue) is in core-shared and always provided; the inner if (queue) below is belt-and-suspenders.
  if (queue) {
    const wrappedAuthUserSignedUp = withSpan(
      tracer,
      { name: "marketing-pages.onAuthUserSignedUpHandler", op: "event-handler" },
      withCapture(
        logger,
        {
          feature: "marketing-pages",
          layer: "event-handler",
          name: "marketing-pages.onAuthUserSignedUpHandler",
        },
        onAuthUserSignedUpHandler(queue),
      ),
    );
    if (marketingPagesContainer.isBound(MARKETING_PAGES_SYMBOLS.IOnAuthUserSignedUpHandler)) {
      marketingPagesContainer.unbind(MARKETING_PAGES_SYMBOLS.IOnAuthUserSignedUpHandler);
    }
    marketingPagesContainer.bind(MARKETING_PAGES_SYMBOLS.IOnAuthUserSignedUpHandler).toConstantValue(wrappedAuthUserSignedUp);
    bus?.subscribe(userSignedUpEvent, "marketing-pages", wrappedAuthUserSignedUp);
  }
  // <gen:jobs>
  const wrappedSendWelcomeEmail = withSpan(
    tracer,
    { name: "marketing-pages.sendWelcomeEmail", op: "job" },
    withCapture(
      logger,
      {
        feature: "marketing-pages",
        layer: "job",
        name: "marketing-pages.sendWelcomeEmail",
      },
      sendWelcomeEmailJob(mailer),
    ),
  );
  if (marketingPagesContainer.isBound(MARKETING_PAGES_SYMBOLS.ISendWelcomeEmailJob)) {
    marketingPagesContainer.unbind(MARKETING_PAGES_SYMBOLS.ISendWelcomeEmailJob);
  }
  marketingPagesContainer.bind(MARKETING_PAGES_SYMBOLS.ISendWelcomeEmailJob).toConstantValue(wrappedSendWelcomeEmail);

  // Register the dev-seed in-memory queue handler so enqueue() actually fires
  // the wrapped job. Production binding skips this — the Payload task does it.
  if (queue && "register" in queue && typeof (queue as { register?: unknown }).register === "function") {
    (queue as { register: (slug: string, h: (input: unknown) => Promise<void>) => void }).register(
      "marketing-pages.send-welcome-email",
      async (input) => {
        const wrapped = marketingPagesContainer.get<ISendWelcomeEmailJob>(
          MARKETING_PAGES_SYMBOLS.ISendWelcomeEmailJob,
        );
        await wrapped(input as SendWelcomeEmailInput);
      },
    );
  }
  void realtime;
  void realtimeRegistry;
  // <gen:realtime-handlers>

  // Boot-time conformance check (dev-seed mode).
  assertFeatureConformance(
    marketingPagesContainer,
    marketingPagesManifest,
    {
      getPageBySlug: MARKETING_PAGES_SYMBOLS.IGetPageBySlugUseCase,
      getSiteSettings: MARKETING_PAGES_SYMBOLS.IGetSiteSettingsUseCase,
    },
    ctx,
  );
}
