import { Router } from "express";
import { employeeFileService } from "../services/employee-files.js";
import { agentService } from "../services/agents.js";
import { assertBoard, assertCompanyAccess } from "./authz.js";
import { notFound } from "../errors.js";
const VALID_FILE_TYPES = ["soul", "procedures", "heartbeat", "memory", "tools", "learning"];
function isValidFileType(value) {
    return VALID_FILE_TYPES.includes(value);
}
export function employeeFileRoutes(db) {
    const router = Router();
    const fileSvc = employeeFileService(db);
    const agents = agentService(db);
    // GET /companies/:companyId/agents/:agentId/brain
    router.get("/companies/:companyId/agents/:agentId/brain", async (req, res) => {
        assertBoard(req);
        assertCompanyAccess(req, req.params.companyId);
        const agent = await agents.getById(req.params.agentId);
        if (!agent || agent.companyId !== req.params.companyId) {
            throw notFound("Agent not found");
        }
        const files = await fileSvc.getAllFiles(agent.id);
        res.json({ files });
    });
    // PUT /companies/:companyId/agents/:agentId/brain/:fileType
    router.put("/companies/:companyId/agents/:agentId/brain/:fileType", async (req, res) => {
        assertBoard(req);
        assertCompanyAccess(req, req.params.companyId);
        const { fileType } = req.params;
        if (!isValidFileType(fileType)) {
            res.status(400).json({ error: `Invalid file type: ${fileType}` });
            return;
        }
        const agent = await agents.getById(req.params.agentId);
        if (!agent || agent.companyId !== req.params.companyId) {
            throw notFound("Agent not found");
        }
        const { content } = req.body;
        if (typeof content !== "string") {
            res.status(400).json({ error: "content must be a string" });
            return;
        }
        await fileSvc.updateFile(agent.id, fileType, content, "user");
        const updated = await fileSvc.getFile(agent.id, fileType);
        res.json({ file: updated });
    });
    return router;
}
//# sourceMappingURL=employee-files.js.map