"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.promptsRouter = void 0;
const drizzle_orm_1 = require("drizzle-orm");
const express_1 = require("express");
const uuid_1 = require("uuid");
const database_1 = require("../../database");
const tables_1 = require("../../database/tables");
const logger_1 = __importDefault(require("../../logger"));
const permissions_1 = require("../../util/config/permissions");
const permission_1 = require("../middlewares/permission");
exports.promptsRouter = (0, express_1.Router)();
exports.promptsRouter.get('/', (0, permission_1.hasPermission)(permissions_1.Permission.PROMPT_LIBRARY_READ), async (req, res) => {
    const db = await (0, database_1.getDb)();
    try {
        const { projectId } = req.query;
        const query = db
            .select({
            id: tables_1.promptsTable.id,
            prompt: tables_1.promptsTable.prompt,
            createdAt: tables_1.promptsTable.createdAt,
            projectId: tables_1.promptsTable.projectId,
        })
            .from(tables_1.promptsTable)
            .orderBy(tables_1.promptsTable.createdAt);
        if (projectId) {
            query.where((0, drizzle_orm_1.eq)(tables_1.promptsTable.projectId, projectId));
        }
        const prompts = await query;
        logger_1.default.info(`Loaded ${prompts.length} prompts`);
        res.json({ prompts });
    }
    catch (error) {
        logger_1.default.error(`Error fetching prompts: ${error}`);
        res.status(500).json({ error: 'Failed to fetch prompts' });
    }
});
exports.promptsRouter.post('/', (0, permission_1.hasPermission)(permissions_1.Permission.PROMPT_LIBRARY_WRITE), async (req, res) => {
    const db = await (0, database_1.getDb)();
    try {
        const { prompt, projectId, orgId } = req.body;
        const id = (0, uuid_1.v4)();
        const [result] = await db
            .insert(tables_1.promptsTable)
            .values({
            id,
            prompt,
            projectId,
            workspaceId: orgId,
        })
            .returning({
            id: tables_1.promptsTable.id,
            createdAt: tables_1.promptsTable.createdAt,
        });
        logger_1.default.info(`Saved prompt ${id}`);
        res.json(result);
    }
    catch (error) {
        logger_1.default.error(`Error saving prompt: ${error}`);
        res.status(500).json({ error: 'Failed to save prompt' });
    }
});
exports.promptsRouter.get('/:promptId', (0, permission_1.hasPermission)(permissions_1.Permission.PROMPT_LIBRARY_READ), async (req, res) => {
    const db = await (0, database_1.getDb)();
    try {
        const { promptId } = req.params;
        const [prompt] = await db
            .select()
            .from(tables_1.promptsTable)
            .where((0, drizzle_orm_1.eq)(tables_1.promptsTable.id, promptId))
            .limit(1);
        if (!prompt) {
            res.status(404).json({ error: 'Prompt not found' });
            return;
        }
        logger_1.default.info(`Fetched prompt ${promptId}`);
        res.json(prompt);
    }
    catch (error) {
        logger_1.default.error(`Error fetching prompt: ${error}`);
        res.status(500).json({ error: 'Failed to fetch prompt' });
    }
});
exports.promptsRouter.patch('/:promptId', (0, permission_1.hasPermission)(permissions_1.Permission.PROMPT_LIBRARY_WRITE), async (req, res) => {
    const db = await (0, database_1.getDb)();
    try {
        const { promptId } = req.params;
        const updateData = {};
        // Only include fields that are present in the request body
        if (req.body.prompt !== undefined) {
            updateData.prompt = req.body.prompt;
        }
        if (req.body.projectId !== undefined) {
            updateData.projectId = req.body.projectId;
        }
        const [result] = await db
            .update(tables_1.promptsTable)
            .set(updateData)
            .where((0, drizzle_orm_1.eq)(tables_1.promptsTable.id, promptId))
            .returning({
            id: tables_1.promptsTable.id,
            updatedAt: tables_1.promptsTable.createdAt,
        });
        if (!result) {
            res.status(404).json({ error: 'Prompt not found' });
            return;
        }
        logger_1.default.info(`Updated prompt ${promptId}`);
        res.json(result);
    }
    catch (error) {
        logger_1.default.error(`Error updating prompt: ${error}`);
        res.status(500).json({ error: 'Failed to update prompt' });
    }
});
exports.promptsRouter.delete('/:promptId', (0, permission_1.hasPermission)(permissions_1.Permission.PROMPT_LIBRARY_WRITE), async (req, res) => {
    const db = await (0, database_1.getDb)();
    try {
        const { promptId } = req.params;
        const [result] = await db
            .delete(tables_1.promptsTable)
            .where((0, drizzle_orm_1.eq)(tables_1.promptsTable.id, promptId))
            .returning({ id: tables_1.promptsTable.id });
        if (!result) {
            res.status(404).json({ error: 'Prompt not found' });
            return;
        }
        logger_1.default.info(`Deleted prompt ${promptId}`);
        res.json({ success: true });
    }
    catch (error) {
        logger_1.default.error(`Error deleting prompt: ${error}`);
        res.status(500).json({ error: 'Failed to delete prompt' });
    }
});
//# sourceMappingURL=prompts.js.map