/**
 * chatbot.repository.ts — CRUD para AICredential e AIBot.
 */
import { prisma } from '../../infra/database/prisma'

// ─── Credentials ─────────────────────────────────────────────────────────────

export const credentialRepo = {
  findAll: (tenantId: string, instanceId: string) =>
    prisma.aICredential.findMany({
      where: { tenantId, instanceId },
      select: { id: true, name: true, provider: true, createdAt: true },
      orderBy: { createdAt: 'desc' },
    }),

  create: (data: {
    tenantId: string
    instanceId: string
    name: string
    provider?: 'GEMINI' | 'OPENAI'
    apiKey: string
  }) =>
    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: string, tenantId: string) =>
    prisma.aICredential.deleteMany({ where: { id, tenantId } }),
}

// ─── Bots ─────────────────────────────────────────────────────────────────────

export const botRepo = {
  findAll: (tenantId: string, instanceId: string) =>
    prisma.aIBot.findMany({
      where: { tenantId, instanceId },
      include: { credential: { select: { id: true, name: true, provider: true } } },
      orderBy: { createdAt: 'desc' },
    }),

  findEnabled: (tenantId: string, instanceId: string) =>
    prisma.aIBot.findFirst({
      where: { tenantId, instanceId, enabled: true },
      include: { credential: true },
    }),

  findById: (id: string, tenantId: string) =>
    prisma.aIBot.findFirst({
      where: { id, tenantId },
      include: { credential: true },
    }),

  create: (data: {
    tenantId: string
    instanceId: string
    credentialId: string
    name: string
    systemPrompt: string
    model?: string
    enabled?: boolean
    triggerMode?: 'ALL' | 'KEYWORD'
    keywords?: string[]
  }) =>
    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: string,
    tenantId: string,
    data: Partial<{
      name: string
      systemPrompt: string
      model: string
      enabled: boolean
      triggerMode: 'ALL' | 'KEYWORD'
      keywords: string[]
      credentialId: string
    }>
  ) =>
    prisma.aIBot.updateMany({ where: { id, tenantId }, data }),

  delete: (id: string, tenantId: string) =>
    prisma.aIBot.deleteMany({ where: { id, tenantId } }),
}

// ─── Chat bot state ───────────────────────────────────────────────────────────

export const chatBotState = {
  pauseBot: (chatId: string) =>
    prisma.chat.update({ where: { id: chatId }, data: { botPaused: true } }),

  resumeBot: (chatId: string) =>
    prisma.chat.update({ where: { id: chatId }, data: { botPaused: false } }),

  updateSummary: (chatId: string, botSummary: string) =>
    prisma.chat.update({ where: { id: chatId }, data: { botSummary } }),

  getState: (chatId: string) =>
    prisma.chat.findUnique({
      where: { id: chatId },
      select: { botPaused: true, botSummary: true },
    }),
}
