import { employeeFiles } from "@zmeel/db";
import { eq, and } from "drizzle-orm";
const FILE_TYPES = ["soul", "procedures", "heartbeat", "memory", "tools", "learning"];
export function employeeFileService(db) {
    return {
        async getFile(agentId, fileType) {
            const rows = await db.select().from(employeeFiles)
                .where(and(eq(employeeFiles.agentId, agentId), eq(employeeFiles.fileType, fileType)));
            return rows[0] ?? null;
        },
        async updateFile(agentId, fileType, content, updatedBy = "system") {
            const existing = await this.getFile(agentId, fileType);
            if (existing) {
                await db.update(employeeFiles)
                    .set({ content, version: existing.version + 1, updatedAt: new Date(), updatedBy })
                    .where(eq(employeeFiles.id, existing.id));
            }
            else {
                // shouldn't happen if initialized, but handle gracefully
                const file = await this.getFile(agentId, fileType);
                if (!file) {
                    // Get companyId from the first file or default
                    await db.insert(employeeFiles).values({
                        agentId, companyId: "", fileType, content, updatedBy
                    });
                }
            }
        },
        async appendToFile(agentId, fileType, appendContent, updatedBy = "agent") {
            const existing = await this.getFile(agentId, fileType);
            const currentContent = existing?.content ?? "";
            const newContent = currentContent + "\n" + appendContent;
            await this.updateFile(agentId, fileType, newContent, updatedBy);
        },
        async initializeFiles(agentId, companyId, defaults) {
            for (const fileType of FILE_TYPES) {
                await db.insert(employeeFiles).values({
                    agentId,
                    companyId,
                    fileType,
                    content: defaults[fileType] ?? "",
                    updatedBy: "system",
                }).onConflictDoNothing();
            }
        },
        async getFullContext(agentId) {
            const files = await db.select().from(employeeFiles)
                .where(eq(employeeFiles.agentId, agentId));
            const fileMap = {};
            for (const f of files) {
                fileMap[f.fileType] = f.content;
            }
            const sections = [
                fileMap.soul ? `# SOUL\n${fileMap.soul}` : "",
                fileMap.procedures ? `# PROCEDURES\n${fileMap.procedures}` : "",
                fileMap.heartbeat ? `# SCHEDULED TASKS\n${fileMap.heartbeat}` : "",
                fileMap.tools ? `# CONNECTED TOOLS\n${fileMap.tools}` : "",
                fileMap.memory ? `# MEMORY\n${fileMap.memory}` : "",
                fileMap.learning ? `# LEARNING\n${fileMap.learning}` : "",
            ].filter(Boolean);
            return sections.join("\n\n---\n\n");
        },
        async getAllFiles(agentId) {
            return db.select().from(employeeFiles)
                .where(eq(employeeFiles.agentId, agentId));
        },
    };
}
//# sourceMappingURL=employee-files.js.map