"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.deletePromptsLibrary = void 0;
const drizzle_orm_1 = require("drizzle-orm");
const database_1 = require("../../../database");
const tables_1 = require("../../../database/tables");
const logger_1 = __importDefault(require("../../../logger"));
const constants_1 = require("../../../constants");
const activityLogService_1 = require("../../services/activityLogService");
const deletePromptsLibrary = async (req, res) => {
    const db = await (0, database_1.getDb)();
    try {
        const { libraryId } = req.params;
        // Get library details before deleting for activity log
        const [library] = await db
            .select()
            .from(tables_1.promptsLibraryTable)
            .where((0, drizzle_orm_1.eq)(tables_1.promptsLibraryTable.id, libraryId));
        if (!library) {
            res.status(404).json({ error: 'Prompt library not found' });
            return;
        }
        await db
            .delete(tables_1.promptsLibraryPromptsTable)
            .where((0, drizzle_orm_1.eq)(tables_1.promptsLibraryPromptsTable.libraryId, libraryId));
        await db
            .delete(tables_1.promptsLibraryTable)
            .where((0, drizzle_orm_1.eq)(tables_1.promptsLibraryTable.id, libraryId));
        logger_1.default.info(`Deleted prompt library ${libraryId}`);
        // Log activity
        try {
            const token = req.headers['x-access-token'] || req.headers.authorization;
            const { userId, userName } = (0, activityLogService_1.getUserInfo)(req);
            if (token && library.workspaceId && typeof library.workspaceId === 'string') {
                const orgId = library.workspaceId;
                const projectId = (library.projectId && typeof library.projectId === 'string') ? library.projectId : '';
                const [orgName, projectName] = await Promise.all([
                    (0, activityLogService_1.fetchOrgName)(token, orgId),
                    projectId ? (0, activityLogService_1.fetchProjectName)(token, orgId, projectId) : Promise.resolve(''),
                ]);
                await (0, activityLogService_1.logActivity)(token, {
                    type: constants_1.ActivityType.DELETE_PROMPT_LIBRARY,
                    params: (0, activityLogService_1.buildActivityParams)({
                        prompt_library_name: { value: library.name ?? '', _id: libraryId },
                        user_name: { value: userName, _id: userId },
                        org_name: { value: orgName || orgId, _id: orgId },
                        project_name: { value: projectName || projectId, _id: projectId || undefined },
                    }),
                    project_id: projectId || undefined,
                    org_id: orgId,
                    created_by: userId,
                    updated_by: userId,
                });
            }
        }
        catch (activityError) {
            logger_1.default.error(`Error logging activity for delete prompts library: ${activityError}`);
        }
        res.json({ success: true });
    }
    catch (error) {
        logger_1.default.error(`Error deleting prompt library: ${error}`);
        res.status(500).json({ error: 'Failed to delete prompt library' });
    }
};
exports.deletePromptsLibrary = deletePromptsLibrary;
//# sourceMappingURL=delete-prompts-library.js.map