import * as plugins from '../../plugins.js';
import { DcRouterDb } from '../classes.dcrouter-db.js';
import type { ISecurityPolicyAuditEvent } from '../../../ts_interfaces/data/security-policy.js';

const getDb = () => DcRouterDb.getInstance().getDb();

@plugins.smartdata.Collection(() => getDb())
export class SecurityPolicyAuditDoc extends plugins.smartdata.SmartDataDbDoc<SecurityPolicyAuditDoc, SecurityPolicyAuditDoc> implements ISecurityPolicyAuditEvent {
  @plugins.smartdata.unI()
  @plugins.smartdata.svDb()
  public id!: string;

  @plugins.smartdata.svDb()
  public action!: string;

  @plugins.smartdata.svDb()
  public actor!: string;

  @plugins.smartdata.svDb()
  public details!: Record<string, unknown>;

  @plugins.smartdata.svDb()
  public createdAt: number = Date.now();

  constructor() {
    super();
  }

  public static async findRecent(limit = 100): Promise<SecurityPolicyAuditDoc[]> {
    const docs = await SecurityPolicyAuditDoc.getInstances({});
    return docs.sort((a, b) => b.createdAt - a.createdAt).slice(0, limit);
  }
}
