import { getDb } from './db.js';
import { listInstalledAgents, uninstallAgent } from './agent-templates.js';
import { listAgentToolPermissions } from './runtime.js';
import { listMemory } from './agent-memory.js';
export { listInstalledAgents, uninstallAgent };
export function getTeamAgent(agentId) {
  const db = getDb();
  const agent = db.prepare(`SELECT a.*, i.template_slug, i.project_id, p.name AS project_name, p.slug AS project_slug FROM agents a LEFT JOIN agent_template_installations i ON i.agent_id = a.id LEFT JOIN projects p ON p.id = i.project_id WHERE a.id = ?`).get(agentId);
  if (!agent) return null;
  const activity = db.prepare('SELECT * FROM activity_log WHERE agent_id = ? ORDER BY created_at DESC LIMIT 10').all(agentId);
  const tasks = db.prepare('SELECT * FROM heartbeat_tasks WHERE agent_id = ? ORDER BY created_at DESC LIMIT 10').all(agentId);
  const artifacts = db.prepare('SELECT * FROM artifacts WHERE agent_id = ? ORDER BY created_at DESC LIMIT 10').all(agentId);
  return { agent, activity, tasks, artifacts, memory: listMemory({ agent_id: agentId, project_id: agent.project_id, engine: agent.engine || 'ollama' }), permissions: listAgentToolPermissions(agentId) };
}
export function patchTeamAgent(agentId, fields = {}) {
  if (fields.status) getDb().prepare('UPDATE agents SET status = ? WHERE id = ?').run(fields.status, agentId);
  return getTeamAgent(agentId);
}
