import "reflect-metadata";
import { afterEach, beforeEach, describe, expect, it } from "vitest";
import { NoopTracer, NoopLogger } from "@repo/core-shared/instrumentation";
import { RecordingEventBus, RecordingJobQueue } from "@repo/core-testing/instrumentation";
import { bindDevSeedBlog } from "@/di/bind-dev-seed";
import { blogContainer } from "@/di/container";
import { BLOG_SYMBOLS } from "@/di/symbols";
import { MockArticlesRepository } from "@/infrastructure/repositories/articles.repository.mock";
import type { IArticlesRepository } from "@/application/repositories/articles.repository.interface";

describe("bindDevSeedBlog", () => {
  const tracer = new NoopTracer();
  const logger = new NoopLogger();

  // Each test starts from the default empty-mock binding and tears down
  // afterwards so the global blogContainer state stays clean for siblings.
  beforeEach(() => {
    for (const sym of Object.values(BLOG_SYMBOLS)) {
      if (blogContainer.isBound(sym)) blogContainer.unbind(sym);
    }
    blogContainer
      .bind<IArticlesRepository>(BLOG_SYMBOLS.IArticlesRepository)
      .to(MockArticlesRepository);
  });

  afterEach(() => {
    for (const sym of Object.values(BLOG_SYMBOLS)) {
      if (blogContainer.isBound(sym)) blogContainer.unbind(sym);
    }
    blogContainer
      .bind<IArticlesRepository>(BLOG_SYMBOLS.IArticlesRepository)
      .to(MockArticlesRepository);
  });

  it("populates the repository with the dev articles", async () => {
    await bindDevSeedBlog({ tracer, logger, bus: new RecordingEventBus(), queue: new RecordingJobQueue() });

    const repo = blogContainer.get<IArticlesRepository>(
      BLOG_SYMBOLS.IArticlesRepository,
    );
    const all = await repo.getArticles();

    expect(all.length).toBeGreaterThan(0);
  });

  it("seeds the welcome article reachable by slug", async () => {
    await bindDevSeedBlog({ tracer, logger, bus: new RecordingEventBus(), queue: new RecordingJobQueue() });

    const repo = blogContainer.get<IArticlesRepository>(
      BLOG_SYMBOLS.IArticlesRepository,
    );
    const welcome = await repo.getArticleBySlug("welcome");

    expect(welcome).toBeDefined();
    expect(welcome?.title).toBe("Welcome to the blog");
    expect(welcome?.status).toBe("published");
  });

  it("is idempotent — calling twice rebuilds a fresh populated repo", async () => {
    await bindDevSeedBlog({ tracer, logger, bus: new RecordingEventBus(), queue: new RecordingJobQueue() });
    const before = blogContainer.get<IArticlesRepository>(
      BLOG_SYMBOLS.IArticlesRepository,
    );
    const beforeCount = (await before.getArticles()).length;

    await bindDevSeedBlog({ tracer, logger, bus: new RecordingEventBus(), queue: new RecordingJobQueue() });
    const after = blogContainer.get<IArticlesRepository>(
      BLOG_SYMBOLS.IArticlesRepository,
    );
    const afterCount = (await after.getArticles()).length;

    expect(afterCount).toBe(beforeCount);
    // It's a fresh instance — not the previous one.
    expect(after).not.toBe(before);
  });
});
