import { db } from '@/lib/db'
import { foundingOwnerId } from '@/lib/owner'

// Append an activity-feed entry. Never let logging failures break the action
// being logged — callers fire-and-forget.
export function logActivity(ownerId: string, type: string, summary: string): void {
  db.activity
    .create({ data: { ownerId, type, summary: summary.slice(0, 300) } })
    .catch(err => console.error('[activity] log failed:', err?.message || err))
}

// Host-monitor transitions and other machine-generated events have no user
// behind them; they belong to the founding account, which is the one with the
// admin surfaces that display them.
export function logSystemActivity(type: string, summary: string): void {
  foundingOwnerId()
    .then(ownerId => logActivity(ownerId, type, summary))
    .catch(err => console.error('[activity] system log failed:', err?.message || err))
}
