import AIAgentTaskLogService from "../Services/AIAgentTaskLogService";
import AIAgentService from "../Services/AIAgentService";
import AIAgentTaskService from "../Services/AIAgentTaskService";
import Response from "../Utils/Response";
import BaseAPI from "./BaseAPI";
import AIAgentTaskLog from "../../Models/DatabaseModels/AIAgentTaskLog";
import BadDataException from "../../Types/Exception/BadDataException";
import ObjectID from "../../Types/ObjectID";
import LogSeverity from "../../Types/Log/LogSeverity";
export default class AIAgentTaskLogAPI extends BaseAPI {
    constructor() {
        var _a;
        super(AIAgentTaskLog, AIAgentTaskLogService);
        /*
         * Create a log entry for an AI Agent task
         * Validates aiAgentId and aiAgentKey before creating log
         */
        this.router.post(`${(_a = new this.entityType().getCrudApiPath()) === null || _a === void 0 ? void 0 : _a.toString()}/create-log`, async (req, res, next) => {
            try {
                const data = req.body;
                /* Validate AI Agent credentials */
                const aiAgent = await this.validateAIAgent(data);
                if (!aiAgent) {
                    return Response.sendErrorResponse(req, res, new BadDataException("Invalid AI Agent ID or AI Agent Key"));
                }
                /* Validate required fields */
                if (!data["taskId"]) {
                    return Response.sendErrorResponse(req, res, new BadDataException("taskId is required"));
                }
                if (!data["severity"]) {
                    return Response.sendErrorResponse(req, res, new BadDataException("severity is required"));
                }
                if (!data["message"]) {
                    return Response.sendErrorResponse(req, res, new BadDataException("message is required"));
                }
                const taskId = new ObjectID(data["taskId"]);
                const severity = data["severity"];
                const message = data["message"];
                /* Validate severity value */
                const validSeverities = Object.values(LogSeverity);
                if (!validSeverities.includes(severity)) {
                    return Response.sendErrorResponse(req, res, new BadDataException(`Invalid severity. Must be one of: ${validSeverities.join(", ")}`));
                }
                /* Check if task exists and get project ID */
                const existingTask = await AIAgentTaskService.findOneById({
                    id: taskId,
                    select: {
                        _id: true,
                        projectId: true,
                    },
                    props: {
                        isRoot: true,
                    },
                });
                if (!existingTask) {
                    return Response.sendErrorResponse(req, res, new BadDataException("Task not found"));
                }
                /* Create the log entry */
                const logEntry = new AIAgentTaskLog();
                logEntry.projectId = existingTask.projectId;
                logEntry.aiAgentTaskId = taskId;
                logEntry.aiAgentId = aiAgent.id;
                logEntry.severity = severity;
                logEntry.message = message;
                await AIAgentTaskLogService.create({
                    data: logEntry,
                    props: {
                        isRoot: true,
                    },
                });
                return Response.sendJsonObjectResponse(req, res, {
                    taskId: taskId.toString(),
                    message: "Log entry created successfully",
                });
            }
            catch (err) {
                next(err);
            }
        });
    }
    /*
     * Validate AI Agent credentials from request body
     * Returns AIAgent if valid, null otherwise
     */
    async validateAIAgent(data) {
        if (!data["aiAgentId"] || !data["aiAgentKey"]) {
            return null;
        }
        const aiAgentId = new ObjectID(data["aiAgentId"]);
        const aiAgentKey = data["aiAgentKey"];
        const aiAgent = await AIAgentService.findOneBy({
            query: {
                _id: aiAgentId.toString(),
                key: aiAgentKey,
            },
            select: {
                _id: true,
            },
            props: {
                isRoot: true,
            },
        });
        return aiAgent;
    }
}
//# sourceMappingURL=AIAgentTaskLogAPI.js.map