import {
  BaseRepository
} from "./chunk-J6KZS54B.js";

// runtime/base-classes/activity-entity-repository.ts
import { eq, between, desc } from "drizzle-orm";
var toCamel = (snake) => snake.replace(/_([a-z0-9])/g, (_, c) => c.toUpperCase());
var ActivityEntityRepository = class extends BaseRepository {
  /**
   * Per-entity Activity config. The template emits this from `config:
   * { Activity: {...} }`; entities that only use date-range / user scoping omit
   * it (and must not call the subject finders).
   */
  patternConfig;
  /**
   * camelCase key for the recency-ordering column. Defaults to `occurredAt`
   * (column `occurred_at`); override via `patternConfig.occurredAt`.
   */
  get occurredAtColumn() {
    const snake = this.patternConfig?.occurredAt ?? "occurred_at";
    return toCamel(snake);
  }
  /**
   * camelCase key for the subject FK column, resolved from `patternConfig`:
   * `subjectColumn` (explicit) → `<subject>_id` (derived). Throws when neither
   * is configured — the subject finders are unusable without it, and a clear
   * error beats a silent `undefined` column index.
   */
  get subjectColumn() {
    const explicit = this.patternConfig?.subjectColumn;
    if (explicit) return toCamel(explicit);
    const subject = this.patternConfig?.subject;
    if (subject) return toCamel(`${subject}_id`);
    throw new Error(
      "ActivityEntityRepository: subject finders require a subject column. Set `config: { Activity: { subject: '<entity>' } }` (\u2192 <entity>_id) or `config: { Activity: { subjectColumn: '<column>' } }` on the entity YAML."
    );
  }
  /**
   * Find activities within a date range (inclusive), by the recency column.
   */
  async findByDateRange(start, end) {
    const rows = await this.baseQuery().where(between(this.table[this.occurredAtColumn], start, end));
    return rows;
  }
  /**
   * Find all activities for a specific user (actor / owner scoping).
   */
  async findByUserId(userId) {
    const rows = await this.baseQuery().where(eq(this.table["userId"], userId));
    return rows;
  }
  /**
   * Find all activities for a specific subject (config-driven FK column).
   */
  async findBySubjectId(subjectId) {
    const rows = await this.baseQuery().where(eq(this.table[this.subjectColumn], subjectId));
    return rows;
  }
  /**
   * Find the most recent activities for a subject, ordered by the recency
   * column descending.
   */
  async findRecentBySubjectId(subjectId, limit = 10) {
    const rows = await this.baseQuery().where(eq(this.table[this.subjectColumn], subjectId)).orderBy(desc(this.table[this.occurredAtColumn])).limit(limit);
    return rows;
  }
};

export {
  ActivityEntityRepository
};
//# sourceMappingURL=chunk-MKWQKKK7.js.map