import { eq, or, type Db, agents } from '@mosaic/db'; export type Agent = typeof agents.$inferSelect; export type NewAgent = typeof agents.$inferInsert; export function createAgentsRepo(db: Db) { return { async findAll(): Promise { return db.select().from(agents); }, async findById(id: string): Promise { const rows = await db.select().from(agents).where(eq(agents.id, id)); return rows[0]; }, async findByName(name: string): Promise { const rows = await db.select().from(agents).where(eq(agents.name, name)); return rows[0]; }, async findByProject(projectId: string): Promise { return db.select().from(agents).where(eq(agents.projectId, projectId)); }, async findSystem(): Promise { return db.select().from(agents).where(eq(agents.isSystem, true)); }, async findAccessible(ownerId: string): Promise { return db .select() .from(agents) .where(or(eq(agents.ownerId, ownerId), eq(agents.isSystem, true))); }, async create(data: NewAgent): Promise { const rows = await db.insert(agents).values(data).returning(); return rows[0]!; }, async update(id: string, data: Partial): Promise { const rows = await db .update(agents) .set({ ...data, updatedAt: new Date() }) .where(eq(agents.id, id)) .returning(); return rows[0]; }, async remove(id: string): Promise { const rows = await db.delete(agents).where(eq(agents.id, id)).returning(); return rows.length > 0; }, }; } export type AgentsRepo = ReturnType;