"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.chatBotState = exports.botRepo = exports.credentialRepo = void 0;
/**
 * chatbot.repository.ts — CRUD para AICredential e AIBot.
 */
const prisma_1 = require("../../infra/database/prisma");
// ─── Credentials ─────────────────────────────────────────────────────────────
exports.credentialRepo = {
    findAll: (tenantId, instanceId) => prisma_1.prisma.aICredential.findMany({
        where: { tenantId, instanceId },
        select: { id: true, name: true, provider: true, createdAt: true },
        orderBy: { createdAt: 'desc' },
    }),
    create: (data) => prisma_1.prisma.aICredential.create({
        data: {
            tenantId: data.tenantId,
            instanceId: data.instanceId,
            name: data.name,
            provider: data.provider ?? 'GEMINI',
            apiKey: data.apiKey,
        },
        select: { id: true, name: true, provider: true, createdAt: true },
    }),
    delete: (id, tenantId) => prisma_1.prisma.aICredential.deleteMany({ where: { id, tenantId } }),
};
// ─── Bots ─────────────────────────────────────────────────────────────────────
exports.botRepo = {
    findAll: (tenantId, instanceId) => prisma_1.prisma.aIBot.findMany({
        where: { tenantId, instanceId },
        include: { credential: { select: { id: true, name: true, provider: true } } },
        orderBy: { createdAt: 'desc' },
    }),
    findEnabled: (tenantId, instanceId) => prisma_1.prisma.aIBot.findFirst({
        where: { tenantId, instanceId, enabled: true },
        include: { credential: true },
    }),
    findById: (id, tenantId) => prisma_1.prisma.aIBot.findFirst({
        where: { id, tenantId },
        include: { credential: true },
    }),
    create: (data) => prisma_1.prisma.aIBot.create({
        data: {
            tenantId: data.tenantId,
            instanceId: data.instanceId,
            credentialId: data.credentialId,
            name: data.name,
            systemPrompt: data.systemPrompt,
            model: data.model ?? 'gemini-1.5-flash',
            enabled: data.enabled ?? false,
            triggerMode: data.triggerMode ?? 'ALL',
            keywords: data.keywords ?? [],
        },
    }),
    update: (id, tenantId, data) => prisma_1.prisma.aIBot.updateMany({ where: { id, tenantId }, data }),
    delete: (id, tenantId) => prisma_1.prisma.aIBot.deleteMany({ where: { id, tenantId } }),
};
// ─── Chat bot state ───────────────────────────────────────────────────────────
exports.chatBotState = {
    pauseBot: (chatId) => prisma_1.prisma.chat.update({ where: { id: chatId }, data: { botPaused: true } }),
    resumeBot: (chatId) => prisma_1.prisma.chat.update({ where: { id: chatId }, data: { botPaused: false } }),
    updateSummary: (chatId, botSummary) => prisma_1.prisma.chat.update({ where: { id: chatId }, data: { botSummary } }),
    getState: (chatId) => prisma_1.prisma.chat.findUnique({
        where: { id: chatId },
        select: { botPaused: true, botSummary: true },
    }),
};
