"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.getPromptsLibraryDetails = 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 getPromptsLibraryDetails = async (req, res) => {
    const db = await (0, database_1.getDb)();
    try {
        const libraryId = req.query.libraryId;
        const projectId = req.query.projectId;
        const name = req.query.name;
        let library;
        if (!projectId) {
            res.status(400).json({ error: 'projectId must be provided' });
            return;
        }
        if (libraryId) {
            [library] = await db
                .select()
                .from(tables_1.promptsLibraryTable)
                .where((0, drizzle_orm_1.and)((0, drizzle_orm_1.eq)(tables_1.promptsLibraryTable.id, libraryId), (0, drizzle_orm_1.eq)(tables_1.promptsLibraryTable.projectId, projectId)))
                .limit(1);
        }
        else if (name) {
            [library] = await db
                .select()
                .from(tables_1.promptsLibraryTable)
                .where((0, drizzle_orm_1.and)((0, drizzle_orm_1.eq)(tables_1.promptsLibraryTable.name, name), (0, drizzle_orm_1.eq)(tables_1.promptsLibraryTable.projectId, projectId)))
                .limit(1);
        }
        else {
            res.status(400).json({ error: 'Either libraryId or name must be provided' });
            return;
        }
        if (!library) {
            res.status(404).json({ error: 'Prompt library not found' });
            return;
        }
        const promptRelations = await db
            .select({ promptId: tables_1.promptsLibraryPromptsTable.promptId })
            .from(tables_1.promptsLibraryPromptsTable)
            .where((0, drizzle_orm_1.eq)(tables_1.promptsLibraryPromptsTable.libraryId, library.id));
        const promptIds = promptRelations.map((rel) => rel.promptId);
        let prompts = [];
        if (promptIds.length > 0) {
            prompts = await db
                .select({
                id: tables_1.promptsTable.id,
                prompt: tables_1.promptsTable.prompt,
                tests: tables_1.promptsTable.tests,
                dataset: tables_1.promptsTable.dataset,
                createdAt: tables_1.promptsTable.createdAt,
                promptType: tables_1.promptsTable.type,
                userIntentions: tables_1.promptsTable.userIntentions ?? [],
                userProfiles: tables_1.promptsTable.userProfiles ?? [],
                openingMessage: tables_1.promptsTable.openingMessage ?? '',
                maximumTurns: tables_1.promptsTable.maximumTurns ?? '',
                minimumTurns: tables_1.promptsTable.minimumTurns ?? '',
                chatStopCriteria: tables_1.promptsTable.chatStopCriteria ?? '',
                conversationFlow: tables_1.promptsTable.conversationFlow ?? [],
                turns: tables_1.promptsTable.turns ?? [],
                conversation_builder_type: tables_1.promptsTable.conversationBuilderType ?? 'call', // Default to 'call' for backward compatibility
            })
                .from(tables_1.promptsTable)
                .where((0, drizzle_orm_1.or)(...promptIds.map((id) => (0, drizzle_orm_1.eq)(tables_1.promptsTable.id, id))));
        }
        const libraryDetails = {
            ...library,
            prompts,
        };
        logger_1.default.info(`Fetched detailed information for prompt library ${libraryId || name}`);
        res.json(libraryDetails);
    }
    catch (error) {
        logger_1.default.error(`Error fetching prompt library details: ${error}`);
        res.status(500).json({ error: 'Failed to fetch prompt library details' });
    }
};
exports.getPromptsLibraryDetails = getPromptsLibraryDetails;
//# sourceMappingURL=get-prompts-library-details.js.map