import { Router } from "express";
import { existsSync, readFileSync, readdirSync } from "fs";
import { join } from "path";
import { agentExists, getAgentWorkspacePath } from "../services/agent-file-service.js";
export const agentMemoryRouter = Router({ mergeParams: true });
/** GET /api/agents/:alias/memory — list daily memory files + curated memory */
agentMemoryRouter.get("/", (req, res) => {
    const alias = req.params.alias;
    if (!agentExists(alias)) {
        res.status(404).json({ error: "Agent not found" });
        return;
    }
    const workspacePath = getAgentWorkspacePath(alias);
    const memoryDir = join(workspacePath, "memory");
    // Read curated long-term memory (MEMORY.md)
    const memoryMdPath = join(workspacePath, "MEMORY.md");
    const curatedMemory = existsSync(memoryMdPath) ? readFileSync(memoryMdPath, "utf-8") : "";
    // List daily journal files
    let dailyFiles = [];
    if (existsSync(memoryDir)) {
        dailyFiles = readdirSync(memoryDir)
            .filter((f) => f.endsWith(".md"))
            .sort()
            .reverse(); // newest first
    }
    res.json({
        curatedMemory,
        dailyFiles,
    });
});
/** GET /api/agents/:alias/memory/:date — read a specific daily memory file */
agentMemoryRouter.get("/:date", (req, res) => {
    const alias = req.params.alias;
    const date = req.params.date;
    if (!agentExists(alias)) {
        res.status(404).json({ error: "Agent not found" });
        return;
    }
    // Validate date format (YYYY-MM-DD)
    if (!/^\d{4}-\d{2}-\d{2}$/.test(date)) {
        res.status(400).json({ error: "Date must be in YYYY-MM-DD format" });
        return;
    }
    const workspacePath = getAgentWorkspacePath(alias);
    const filePath = join(workspacePath, "memory", `${date}.md`);
    if (!existsSync(filePath)) {
        res.json({ date, content: "" });
        return;
    }
    const content = readFileSync(filePath, "utf-8");
    res.json({ date, content });
});
