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

import type { I{{pascalCase entity}}Repository } from "../../application/repositories/{{kebabCase entity}}.repository.interface";
import type { {{pascalCase entity}} } from "../../entities/models/{{kebabCase entity}}";

const FEATURE = "{{kebabCase name}}" as const;
const REPO = "{{kebabCase entity}}" as const;

/**
 * Phase-1 scaffold — the Payload collection has not been added yet, so this
 * repository emits a span but always resolves to `null`. Once you add the
 * collection at `integrations/cms/collections/{{kebabCase entity}}.ts` and
 * register it with Payload, replace the stub below with a real `payload.find`
 * call (see `packages/blog/src/infrastructure/repositories/articles.repository.ts`
 * for the canonical pattern).
 */
@injectable()
export class {{pascalCase entity}}Repository implements I{{pascalCase entity}}Repository {
  private config: SanitizedConfig;
  private tracer: ITracer;
  private logger: ILogger;

  constructor(
    config: SanitizedConfig,
    tracer: ITracer = new NoopTracer(),
    logger: ILogger = new NoopLogger(),
  ) {
    this.config = config;
    this.tracer = tracer;
    this.logger = logger;
    void this.config;
  }

  async get{{pascalCase entity}}(id: string): Promise<{{pascalCase entity}} | null> {
    return this.tracer.startSpan(
      { name: "{{camelCase entity}}.get{{pascalCase entity}}", op: "repository", attributes: {} },
      async (span) => {
        try {
          // TODO: replace with `payload.find({ collection: "{{entityPlural}}", where: { id: { equals: id } } })`
          // once the Payload collection is registered.
          span.setAttribute("id", id);
          span.setAttribute("found", false);
          return null;
        } catch (err) {
          this.logger.captureException(err, {
            tags: { feature: FEATURE, repo: REPO, method: "get{{pascalCase entity}}" },
          });
          span.setStatus("error", err instanceof Error ? err.message : String(err));
          throw err;
        }
      },
    );
  }
}
