import crypto from 'crypto';
import { getConnection } from '../../../modules/database/connection.js';
function generateId() {
    return 'pt_' + crypto.randomBytes(12).toString('hex');
}
export const promptTemplatesDb = {
    getAllByUser(userId) {
        const db = getConnection();
        return db
            .prepare('SELECT * FROM prompt_templates WHERE user_id = ? ORDER BY sort_order, name')
            .all(userId);
    },
    getById(id, userId) {
        const db = getConnection();
        return db
            .prepare('SELECT * FROM prompt_templates WHERE id = ? AND user_id = ?')
            .get(id, userId);
    },
    create(userId, params) {
        const db = getConnection();
        const id = generateId();
        const template = {
            id,
            user_id: userId,
            name: params.name.trim(),
            content: params.content,
            description: params.description || null,
            category: params.category || 'general',
            sort_order: params.sortOrder ?? 0,
            created_at: new Date().toISOString(),
            updated_at: new Date().toISOString(),
        };
        db.prepare(`INSERT INTO prompt_templates (id, user_id, name, content, description, category, sort_order)
       VALUES (?, ?, ?, ?, ?, ?, ?)`).run(template.id, template.user_id, template.name, template.content, template.description, template.category, template.sort_order);
        return template;
    },
    update(id, userId, params) {
        const db = getConnection();
        const existing = promptTemplatesDb.getById(id, userId);
        if (!existing)
            return false;
        const name = params.name !== undefined ? params.name.trim() : existing.name;
        const content = params.content !== undefined ? params.content : existing.content;
        const description = params.description !== undefined ? params.description : existing.description;
        const category = params.category !== undefined ? params.category : existing.category;
        const sortOrder = params.sortOrder !== undefined ? params.sortOrder : existing.sort_order;
        const result = db
            .prepare(`UPDATE prompt_templates
         SET name = ?, content = ?, description = ?, category = ?, sort_order = ?, updated_at = CURRENT_TIMESTAMP
         WHERE id = ? AND user_id = ?`)
            .run(name, content, description, category, sortOrder, id, userId);
        return result.changes > 0;
    },
    delete(id, userId) {
        const db = getConnection();
        const result = db
            .prepare('DELETE FROM prompt_templates WHERE id = ? AND user_id = ?')
            .run(id, userId);
        return result.changes > 0;
    },
};
//# sourceMappingURL=prompt-templates.js.map