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 { blogManifest } from "../feature.manifest.js";
import { blogContainer } from "./container.js";
import { BLOG_SYMBOLS } from "./symbols.js";
import { MockArticlesRepository } from "../infrastructure/repositories/articles.repository.mock.js";
import { buildDevArticles } from "../__seeds__/dev.js";
import { getArticlesUseCase } from "../application/use-cases/get-articles.use-case.js";
import { getArticleBySlugUseCase } from "../application/use-cases/get-article-by-slug.use-case.js";
import { createArticleUseCase } from "../application/use-cases/create-article.use-case.js";
import { getArticlesController } from "../interface-adapters/controllers/get-articles.controller.js";
import { getArticleBySlugController } from "../interface-adapters/controllers/get-article-by-slug.controller.js";
import { createArticleController } from "../interface-adapters/controllers/create-article.controller.js";
import type { IArticlesRepository } from "../application/repositories/articles.repository.interface.js";

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

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

  if (blogContainer.isBound(BLOG_SYMBOLS.IArticlesRepository)) {
    blogContainer.unbind(BLOG_SYMBOLS.IArticlesRepository);
  }
  const repo = new MockArticlesRepository(tracer, logger);
  for (const article of buildDevArticles()) {
    await repo.createArticle(article);
  }
  blogContainer
    .bind<IArticlesRepository>(BLOG_SYMBOLS.IArticlesRepository)
    .toConstantValue(repo);

  // Wrap use cases + controllers identically to bind-production
  const wrappedGetArticles = withSpan(
    tracer,
    { name: "blog.getArticles", op: "use-case" },
    withCapture(
      logger,
      { feature: "blog", layer: "use-case", name: "blog.getArticles" },
      getArticlesUseCase(repo),
    ),
  );
  const wrappedGetArticleBySlug = withSpan(
    tracer,
    { name: "blog.getArticleBySlug", op: "use-case" },
    withCapture(
      logger,
      { feature: "blog", layer: "use-case", name: "blog.getArticleBySlug" },
      getArticleBySlugUseCase(repo),
    ),
  );
  const wrappedCreateArticle = withSpan(
    tracer,
    { name: "blog.createArticle", op: "use-case" },
    withCapture(
      logger,
      { feature: "blog", layer: "use-case", name: "blog.createArticle" },
      createArticleUseCase(repo),
    ),
  );

  for (const sym of [
    BLOG_SYMBOLS.IGetArticlesUseCase,
    BLOG_SYMBOLS.IGetArticleBySlugUseCase,
    BLOG_SYMBOLS.ICreateArticleUseCase,
    BLOG_SYMBOLS.IGetArticlesController,
    BLOG_SYMBOLS.IGetArticleBySlugController,
    BLOG_SYMBOLS.ICreateArticleController,
  ]) {
    if (blogContainer.isBound(sym)) blogContainer.unbind(sym);
  }
  blogContainer.bind(BLOG_SYMBOLS.IGetArticlesUseCase).toConstantValue(wrappedGetArticles);
  blogContainer
    .bind(BLOG_SYMBOLS.IGetArticleBySlugUseCase)
    .toConstantValue(wrappedGetArticleBySlug);
  blogContainer.bind(BLOG_SYMBOLS.ICreateArticleUseCase).toConstantValue(wrappedCreateArticle);

  blogContainer
    .bind(BLOG_SYMBOLS.IGetArticlesController)
    .toConstantValue(
      withSpan(
        tracer,
        { name: "blog.getArticles", op: "controller" },
        withCapture(
          logger,
          { feature: "blog", layer: "controller", name: "blog.getArticles" },
          getArticlesController(wrappedGetArticles),
        ),
      ),
    );
  blogContainer
    .bind(BLOG_SYMBOLS.IGetArticleBySlugController)
    .toConstantValue(
      withSpan(
        tracer,
        { name: "blog.getArticleBySlug", op: "controller" },
        withCapture(
          logger,
          { feature: "blog", layer: "controller", name: "blog.getArticleBySlug" },
          getArticleBySlugController(wrappedGetArticleBySlug),
        ),
      ),
    );
  blogContainer
    .bind(BLOG_SYMBOLS.ICreateArticleController)
    .toConstantValue(
      withSpan(
        tracer,
        { name: "blog.createArticle", op: "controller" },
        withCapture(
          logger,
          { feature: "blog", layer: "controller", name: "blog.createArticle" },
          createArticleController(wrappedCreateArticle),
        ),
      ),
    );
  // bus + queue are accept-and-forward in Phase 6; consumed by Phase 7 generator
  // output at the <gen:event-handlers> / <gen:jobs> anchors below.
  void bus;
  void queue;
  void realtime;
  void realtimeRegistry;
  // <gen:event-handlers>
  // <gen:jobs>
  // <gen:realtime-handlers>

  // Boot-time conformance check (dev-seed mode).
  assertFeatureConformance(
    blogContainer,
    blogManifest,
    {
      getArticles: BLOG_SYMBOLS.IGetArticlesUseCase,
      getArticleBySlug: BLOG_SYMBOLS.IGetArticleBySlugUseCase,
      createArticle: BLOG_SYMBOLS.ICreateArticleUseCase,
    },
    ctx,
  );
}
