import {
  withSpan,
  withCapture,
  INSTRUMENTATION_SYMBOLS,
  type ITracer,
  type ILogger,
} from "@repo/core-shared/instrumentation";
import type { BindContext } from "@repo/core-shared/di";
import { {{camelCase name}}Container } from "./container.js";
import { {{constantCase name}}_SYMBOLS } from "./symbols.js";
import { Mock{{pascalCase entity}}Repository } from "../infrastructure/repositories/{{kebabCase entity}}.repository.mock.js";
import { buildDev{{pascalCase entity}}Map } from "../__seeds__/dev.js";
import { get{{pascalCase entity}}UseCase } from "../application/use-cases/get-{{kebabCase entity}}.use-case.js";
import { get{{pascalCase entity}}Controller } from "../interface-adapters/controllers/get-{{kebabCase entity}}.controller.js";
import type { I{{pascalCase entity}}Repository } from "../application/repositories/{{kebabCase entity}}.repository.interface.js";

/**
 * Replace the default mock with a populated one for dev mode + storybook.
 *
 * Mutually exclusive with `bindProduction{{pascalCase name}}(ctx)`.
 * Tests must NOT call this — they construct `new Mock{{pascalCase entity}}Repository()`
 * 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 bindDevSeed{{pascalCase name}}(ctx: BindContext): Promise<void> {
  const { tracer, logger, bus, queue, realtime, realtimeRegistry } = ctx;
  void bus; void queue; void realtime; void realtimeRegistry;

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

  if ({{camelCase name}}Container.isBound({{constantCase name}}_SYMBOLS.I{{pascalCase entity}}Repository)) {
    {{camelCase name}}Container.unbind({{constantCase name}}_SYMBOLS.I{{pascalCase entity}}Repository);
  }
  const repo = new Mock{{pascalCase entity}}Repository(buildDev{{pascalCase entity}}Map(), tracer, logger);
  {{camelCase name}}Container
    .bind<I{{pascalCase entity}}Repository>({{constantCase name}}_SYMBOLS.I{{pascalCase entity}}Repository)
    .toConstantValue(repo);

  // Wrap use case + controller identically to bind-production
  const wrappedGet{{pascalCase entity}} = withSpan(
    tracer,
    { name: "{{camelCase name}}.get{{pascalCase entity}}", op: "use-case" },
    withCapture(
      logger,
      { feature: "{{kebabCase name}}", layer: "use-case", name: "{{camelCase name}}.get{{pascalCase entity}}" },
      get{{pascalCase entity}}UseCase(repo),
    ),
  );

  for (const sym of [
    {{constantCase name}}_SYMBOLS.IGet{{pascalCase entity}}UseCase,
    {{constantCase name}}_SYMBOLS.IGet{{pascalCase entity}}Controller,
  ]) {
    if ({{camelCase name}}Container.isBound(sym)) {{camelCase name}}Container.unbind(sym);
  }
  {{camelCase name}}Container
    .bind({{constantCase name}}_SYMBOLS.IGet{{pascalCase entity}}UseCase)
    .toConstantValue(wrappedGet{{pascalCase entity}});

  {{camelCase name}}Container
    .bind({{constantCase name}}_SYMBOLS.IGet{{pascalCase entity}}Controller)
    .toConstantValue(
      withSpan(
        tracer,
        { name: "{{camelCase name}}.get{{pascalCase entity}}", op: "controller" },
        withCapture(
          logger,
          { feature: "{{kebabCase name}}", layer: "controller", name: "{{camelCase name}}.get{{pascalCase entity}}" },
          get{{pascalCase entity}}Controller(wrappedGet{{pascalCase entity}}),
        ),
      ),
    );
  // <gen:event-handlers>
  // <gen:jobs>
  // <gen:realtime-handlers>
}
