import { mutateWithRLS } from "../db.js";
import { APP_CONFIG_MERGED } from "../config/index.js";
const DEFAULT_RETENTION_DAYS = 7;
async function cleanupOldActions(retentionDays) {
  var _a;
  const effectiveRetentionDays = retentionDays ?? ((_a = APP_CONFIG_MERGED.scheduledActions) == null ? void 0 : _a.retentionDays) ?? DEFAULT_RETENTION_DAYS;
  console.log(`[ScheduledActions] Cleaning up actions older than ${effectiveRetentionDays} days`);
  const result = await mutateWithRLS(
    `DELETE FROM "scheduled_actions"
     WHERE status IN ('completed', 'failed')
       AND "completedAt" < NOW() - INTERVAL '1 day' * $1`,
    [effectiveRetentionDays],
    null
    // System operation
  );
  const deletedCount = result.rowCount;
  console.log(`[ScheduledActions] Cleaned up ${deletedCount} old action(s)`);
  return deletedCount;
}
export {
  cleanupOldActions
};
