import "reflect-metadata";
import { injectable } from "inversify";
import {
  NoopTracer,
  NoopLogger,
  type ITracer,
  type ILogger,
} from "@repo/core-shared/instrumentation";

import type { IPagesRepository } from "../../application/repositories/pages.repository.interface";
import type { Page } from "../../entities/models/page";

const SEED_DATE = new Date("2026-01-01T00:00:00.000Z");

const DEFAULT_SEED: Page[] = [
  {
    id: "p1",
    title: "About",
    slug: "about",
    hero: { heading: "About us" },
    layout: [],
    status: "published",
    publishedAt: SEED_DATE,
    seo: { title: "About — My App" },
    createdAt: SEED_DATE,
    updatedAt: SEED_DATE,
  },
];

@injectable()
export class MockPagesRepository implements IPagesRepository {
  private _pages: Page[];
  private tracer: ITracer;
  private logger: ILogger;

  constructor(
    initialPages: Page[] = DEFAULT_SEED,
    tracer: ITracer = new NoopTracer(),
    logger: ILogger = new NoopLogger(),
  ) {
    this._pages = [...initialPages];
    this.tracer = tracer;
    this.logger = logger;
    void this.logger; // currently unused; reserved for future mock-thrown captures
  }

  async getPageBySlug(slug: string): Promise<Page | undefined> {
    return this.tracer.startSpan(
      { name: "pages.getPageBySlug", op: "repository", attributes: { slug } },
      async (span) => {
        const found = this._pages.find((p) => p.slug === slug);
        span.setAttribute("found", Boolean(found));
        return found;
      },
    );
  }

  async getPages(options?: {
    status?: string;
    limit?: number;
    offset?: number;
  }): Promise<Page[]> {
    return this.tracer.startSpan(
      {
        name: "pages.getPages",
        op: "repository",
        attributes: {
          status: options?.status ?? null,
          limit: options?.limit ?? null,
          offset: options?.offset ?? null,
        },
      },
      async (span) => {
        let result = [...this._pages];
        if (options?.status) {
          result = result.filter((p) => p.status === options.status);
        }
        const offset = options?.offset ?? 0;
        const limit = options?.limit ?? 50;
        const page = result.slice(offset, offset + limit);
        span.setAttribute("count", page.length);
        return page;
      },
    );
  }
}
