import { Router } from "express";
import { agentExists, getAgent } from "../services/agent-file-service.js";
import { getActivity, appendActivity } from "../services/agent-activity.js";
import { getAllEvents } from "../services/event-log.js";
import { resolveAgentKeyAlias } from "../services/agent-settings.js";
export const agentActivityRouter = Router({ mergeParams: true });
/** GET /api/agents/:alias/activity — list activity entries with optional type filter.
 *  Global proxy events are merged into the timeline as type="event" entries. */
agentActivityRouter.get("/", (req, res) => {
    const alias = req.params.alias;
    if (!agentExists(alias)) {
        res.status(404).json({ error: "Agent not found" });
        return;
    }
    const type = req.query.type;
    const limit = req.query.limit ? parseInt(req.query.limit, 10) : 100;
    const offset = req.query.offset ? parseInt(req.query.offset, 10) : 0;
    const validTypes = ["chat", "event", "cron", "connection", "system", "trigger"];
    if (type && !validTypes.includes(type)) {
        res.status(400).json({ error: `type must be one of: ${validTypes.join(", ")}` });
        return;
    }
    // Get agent-specific activity (all types to merge properly, we'll filter after)
    const agentEntries = getActivity(alias, { limit: 10000 });
    // Merge global proxy events as "event" type entries (unless filtering to a non-event type)
    let merged = [...agentEntries];
    if (!type || type === "event") {
        const agentRaw = getAgent(alias);
        const agent = agentRaw ? resolveAgentKeyAlias(agentRaw) : undefined;
        const callerAlias = agent?.mcpKeyAlias;
        const globalEvents = callerAlias ? getAllEvents(callerAlias, { limit: 10000 }) : [];
        const eventEntries = globalEvents.map((e) => ({
            id: `proxy-${e.source}-${e.id}`,
            type: "event",
            message: `${e.source}:${e.eventType}`,
            timestamp: e.storedAt,
            metadata: {
                eventId: e.id,
                eventSource: e.source,
                eventType: e.eventType,
                receivedAt: e.receivedAt,
                eventData: typeof e.data === "string" ? e.data.slice(0, 500) : JSON.stringify(e.data).slice(0, 500),
            },
        }));
        merged.push(...eventEntries);
    }
    // Filter by type if specified
    if (type) {
        merged = merged.filter((e) => e.type === type);
    }
    // Sort newest first
    merged.sort((a, b) => b.timestamp - a.timestamp);
    // Apply pagination
    const entries = merged.slice(offset, offset + limit);
    res.json({ entries });
});
/** POST /api/agents/:alias/activity — append a new activity entry */
agentActivityRouter.post("/", (req, res) => {
    const alias = req.params.alias;
    if (!agentExists(alias)) {
        res.status(404).json({ error: "Agent not found" });
        return;
    }
    const { type, message, metadata } = req.body;
    if (!type || !message) {
        res.status(400).json({ error: "type and message are required" });
        return;
    }
    const validTypes = ["chat", "event", "cron", "connection", "system", "trigger"];
    if (!validTypes.includes(type)) {
        res.status(400).json({ error: `type must be one of: ${validTypes.join(", ")}` });
        return;
    }
    const entry = appendActivity(alias, { type, message, metadata });
    res.status(201).json({ entry });
});
