// apps/web-next/src/server/bind-production.ts
// SERVER-ONLY: this module imports Payload config and must never be bundled into the browser.
import "reflect-metadata";
import { Container } from "inversify";
import { getPayload } from "payload";
import config from "@repo/core-cms";
import {
  bindNoopInstrumentation,
  bindOtelInstrumentation,
  type ITracer,
  type ILogger,
} from "@repo/core-shared/instrumentation";
import type { BindProductionContext, BindContext } from "@repo/core-shared/di";
import {
  InMemoryJobQueue,
  PayloadJobQueue,
  type IJobQueue,
} from "@repo/core-shared/jobs";
import { bindProductionBlog } from "@repo/blog/di/bind-production";
import { bindProductionAuth } from "@repo/auth/di/bind-production";
import { bindProductionMarketingPages } from "@repo/marketing-pages/di/bind-production";
import { bindProductionNavigation } from "@repo/navigation/di/bind-production";
import { bindProductionMedia } from "@repo/media/di/bind-production";
import { bindDevSeedBlog } from "@repo/blog/di/bind-dev-seed";
import { bindDevSeedAuth } from "@repo/auth/di/bind-dev-seed";
import { bindDevSeedMarketingPages } from "@repo/marketing-pages/di/bind-dev-seed";
import { bindDevSeedNavigation } from "@repo/navigation/di/bind-dev-seed";
import { bindDevSeedMedia } from "@repo/media/di/bind-dev-seed";

let bound = false;

// Shared container holds TRACER + LOGGER bindings; per-feature containers
// receive references via parameter passing. This separates the instrumentation
// container (one) from feature containers (per-feature, ADR-008).
const sharedContainer = new Container();

let resolvedTracer: ITracer | null = null;
let resolvedLogger: ILogger | null = null;
let resolvedQueue: IJobQueue | null = null;

/** Rule 0: pick instrumentation backend from DSN env (orthogonal to repo mode). */
function resolveInstrumentation(): { tracer: ITracer; logger: ILogger } {
  if (resolvedTracer && resolvedLogger) {
    return { tracer: resolvedTracer, logger: resolvedLogger };
  }
  const dsn = process.env.WEB_NEXT_SENTRY_DSN;
  const result = dsn
    ? bindOtelInstrumentation(sharedContainer, { dsn, app: "web-next" })
    : bindNoopInstrumentation(sharedContainer);
  resolvedTracer = result.tracer;
  resolvedLogger = result.logger;
  return result;
}

/**
 * Production-mode job queue: backed by Payload's job system so ad-hoc jobs go
 * through `PayloadJobQueue.enqueue`. Cached after first resolution.
 *
 * Note: @repo/core-events (IEventBus) is optional — scaffold via
 * `pnpm turbo gen core-package events` to re-enable cross-feature event fanout.
 */
async function resolveJobsProduction(): Promise<{ queue: IJobQueue }> {
  if (resolvedQueue) return { queue: resolvedQueue };
  const resolvedConfig = await config;
  const payload = await getPayload({ config: resolvedConfig });
  const queue = new PayloadJobQueue(payload);
  resolvedQueue = queue;
  return { queue };
}

/**
 * Dev-seed mode: in-process job queue. Per-feature binders register their job
 * handlers via `queue.register(slug, handler)` at bind time so dev/test
 * exercises the enqueue path without booting Payload.
 */
function resolveJobsDevSeed(): { queue: IJobQueue } {
  if (resolvedQueue) return { queue: resolvedQueue };
  const queue = new InMemoryJobQueue();
  resolvedQueue = queue;
  return { queue };
}

/**
 * Production path: swap each feature's mock repository binding for the real
 * Payload-backed one. Constructs `new XRepository(config, tracer, logger)` per
 * feature as Phase E feature wiring lands (blog: task 18; remaining: tasks 19–22).
 */
export async function bindAllProduction(): Promise<void> {
  if (bound) return;
  bound = true;
  const { tracer, logger } = resolveInstrumentation(); // Rule 0
  const { queue } = await resolveJobsProduction();
  const resolvedConfig = await config;

  const ctx: BindProductionContext = {
    config: resolvedConfig,
    tracer,
    logger,
    queue,
  };

  bindProductionAuth(ctx); // Phase E task 19
  bindProductionBlog(ctx); // Phase E task 18
  bindProductionMarketingPages(ctx); // Phase E task 20
  bindProductionNavigation(ctx); // Phase E task 21
  bindProductionMedia(ctx); // Phase E task 22
}

/**
 * Dev-seed path: keep each feature's MockXRepository in place but populate it
 * with realistic seed data so the running app shows non-empty UI without
 * Payload booted. Mutually exclusive with `bindAllProduction()`.
 */
export async function bindAllDevSeed(): Promise<void> {
  if (bound) return;
  bound = true;
  const { tracer, logger } = resolveInstrumentation(); // Rule 0
  const { queue } = resolveJobsDevSeed();

  const ctx: BindContext = {
    tracer,
    logger,
    queue,
  };

  await bindDevSeedAuth(ctx); // Phase E task 19
  await bindDevSeedBlog(ctx); // Phase E task 18
  await bindDevSeedMarketingPages(ctx); // Phase E task 20
  await bindDevSeedNavigation(ctx); // Phase E task 21
  await bindDevSeedMedia(ctx); // Phase E task 22
}

/**
 * Boot dispatcher: pick the binder based on the environment.
 *
 * Resolution order (first match wins):
 *
 *   Rule 0 (always): instrumentation (Noop vs Sentry) from WEB_NEXT_SENTRY_DSN
 *                    presence — runs inside both bindAllProduction and
 *                    bindAllDevSeed via resolveInstrumentation().
 *   Rule 1: USE_DEV_SEED === "true"  → dev seed (explicit override)
 *   Rule 2: NODE_ENV === "production" → real Payload via bindAllProduction
 *   Rule 3: otherwise                  → dev seed (developer-friendly default)
 *
 * When @repo/core-events is scaffolded via `pnpm turbo gen core-package events`,
 * extend to construct IEventBus and pass it via ctx.bus to per-feature binders.
 * When @repo/core-realtime is scaffolded, extend to accept realtime deps
 * (IRealtimeBroadcaster, IRealtimeHandlerRegistry) and pass them through.
 */
export async function bindAll(): Promise<void> {
  if (process.env.USE_DEV_SEED === "true") {
    await bindAllDevSeed();
    return;
  }
  if (process.env.NODE_ENV === "production") {
    await bindAllProduction();
    return;
  }
  await bindAllDevSeed();
}

/** Test-only resets — not exported via package. Used by bind-production.test.ts. */
export function __resetBindStateForTests(): void {
  bound = false;
  resolvedTracer = null;
  resolvedLogger = null;
  resolvedQueue = null;
}

/** Test-only accessor for resolved instrumentation. */
export function __getInstrumentationForTests(): {
  tracer: ITracer | null;
  logger: ILogger | null;
} {
  return { tracer: resolvedTracer, logger: resolvedLogger };
}
