/**
 * Rotas de Chats — API REST para a inbox do frontend.
 *
 * Endpoints:
 *   GET    /api/chats                          — Lista chats (com filtros e último msg)
 *   GET    /api/chats/search/messages           — Busca full-text em mensagens
 *   GET    /api/chats/stats/overview            — Contagens para o dashboard
 *   GET    /api/chats/:id                       — Detalhe de um chat
 *   POST   /api/chats/:id/read                  — Zera unread_count
 *   PATCH  /api/chats/:id/archive               — Arquiva/desarquiva
 *   PATCH  /api/chats/:id/pin                   — Fixa/desfixa
 *   DELETE /api/chats/:id                       — Remove chat e mensagens
 *   GET    /api/chats/:id/messages              — Histórico paginado por cursor
 *   GET    /api/chats/:id/protocol              — Protocolos do chat
 *   POST   /api/chats/:id/protocol              — Abre novo protocolo
 *   PATCH  /api/chats/:chatId/protocol/:protocolId — Atualiza protocolo
 *
 * @see ChatRepository — queries otimizadas com batch DISTINCT ON
 * @see MessageRepository — busca e paginação de mensagens
 */
import { Router, type Request, type Response } from 'express'
import { z } from 'zod'
import { ChatRepository } from './chat.repository'
import { MessageRepository } from './message.repository'
import { prisma } from '../../infra/database/prisma'
import { dragonfly } from '../../infra/cache/dragonfly'
import { invalidateChatListCache } from './chat.cache'
import type { WhatsAppConnectionManager } from '../whatsapp/connection/WhatsAppConnectionManager'

const chatRepo = new ChatRepository()
const msgRepo = new MessageRepository()

/** TTL do cache de listagem de chats — longo para estabilidade, invalidado por eventos e mutações */
const CHAT_LIST_CACHE_TTL_S = 86400

/** Handler genérico de erros: diferencia ZodError (400) de erros internos (500) */
function handleError(res: Response, err: unknown) {
  if (err instanceof z.ZodError) {
    res.status(400).json({ error: 'Dados inválidos', details: err.flatten().fieldErrors })
    return
  }
  res.status(500).json({ error: (err as Error).message ?? 'Erro interno' })
}

/** Constrói a chave de cache da listagem. Inclui search/archived/limit. */
function chatListCacheKey(tenantId: string, instanceId: string, archived: boolean, limit: number): string {
  return `chats:list:${tenantId}:${instanceId}:${archived ? '1' : '0'}:${limit}`
}

export function buildChatRoutes(manager: WhatsAppConnectionManager): Router {
  const router = Router()

  // ═══════════════════════════════════════════════════════════════════════════
  //  GET /api/chats — Lista chats com snapshot da última mensagem
  // ═══════════════════════════════════════════════════════════════════════════

  router.get('/', async (req: Request, res: Response) => {
    try {
      const { instanceId, search, archived, limit } = z.object({
        instanceId: z.string().uuid(),
        search: z.string().optional(),
        archived: z.coerce.boolean().optional(),
        limit: z.coerce.number().int().min(1).max(200).optional(),
      }).parse(req.query)

      const archivedFlag = archived ?? false
      const limitVal = limit ?? 200
      const tenantId = req.tenantId!

      // ── Cache hit: só quando não houver search (busca tem termos únicos) ──
      const useCache = !search
      let cacheKey: string | null = null
      if (useCache) {
        cacheKey = chatListCacheKey(tenantId, instanceId, archivedFlag, limitVal)
        const cached = await dragonfly.getJson<unknown[]>(cacheKey)
        if (cached) {
          res.setHeader('X-Cache', 'HIT')
          res.json(cached)
          return
        }
      }

      const chats = await chatRepo.findAllByInstance(tenantId, instanceId, {
        search,
        archived,
        limit,
      })

      const result = chats.map((c) => {
        const contact = c.contact
        const isGroup = c.jid.endsWith('@g.us')

        const lastMsg = c.messages[0]
        const activeProtocol = c.protocols[0] ?? null

        const resolvedName = isGroup
          ? (c.name ?? null)
          : (contact?.name ?? contact?.verifiedName ?? contact?.notify
             ?? (lastMsg && !lastMsg.fromMe ? lastMsg.pushName : null)
             ?? null)

        return {
          id: c.id,
          instanceId: c.instanceId,
          jid: c.jid,
          name: c.name ?? null,
          isPinned: c.isPinned,
          isArchived: c.isArchived,
          unreadCount: c.unreadCount,
          lastMessageAt: c.lastMessageAt,
          createdAt: c.createdAt,
          contact: contact
            ? {
                name: resolvedName,
                phone: contact.phone,
                avatarUrl: contact.avatarUrl,
                scoreReputacao: contact.scoreReputacao,
                flagRestricao: contact.flagRestricao,
              }
            : isGroup
            ? { name: resolvedName, phone: null, avatarUrl: null, scoreReputacao: 0, flagRestricao: false }
            : null,
          lastMessage: lastMsg
            ? {
                body: lastMsg.body,
                fromMe: lastMsg.fromMe,
                status: lastMsg.status,
                type: lastMsg.type,
                timestamp: lastMsg.timestamp,
                pushName: lastMsg.fromMe ? null : (lastMsg.pushName ?? null),
              }
            : null,
          protocol: activeProtocol
            ? { number: activeProtocol.number, status: activeProtocol.status }
            : null,
        }
      })

      if (useCache && cacheKey) {
        // fire-and-forget — não bloqueia a resposta
        dragonfly.setJson(cacheKey, result, CHAT_LIST_CACHE_TTL_S).catch(() => {})
      }
      res.setHeader('X-Cache', 'MISS')
      res.json(result)
    } catch (err) {
      handleError(res, err)
    }
  })

  // ═══════════════════════════════════════════════════════════════════════════
  //  GET /api/chats/search/messages
  // ═══════════════════════════════════════════════════════════════════════════
  router.get('/search/messages', async (req: Request, res: Response) => {
    try {
      const { instanceId, q, limit } = z.object({
        instanceId: z.string().uuid(),
        q: z.string().min(2),
        limit: z.coerce.number().int().min(1).max(50).optional(),
      }).parse(req.query)

      const results = await msgRepo.search(req.tenantId!, instanceId, q, limit)
      res.json(results)
    } catch (err) {
      handleError(res, err)
    }
  })

  // ═══════════════════════════════════════════════════════════════════════════
  //  GET /api/chats/stats/overview
  // ═══════════════════════════════════════════════════════════════════════════
  router.get('/stats/overview', async (req: Request, res: Response) => {
    try {
      const { instanceId } = z.object({ instanceId: z.string().uuid() }).parse(req.query)

      const cacheKey = `stats:${req.tenantId}:${instanceId}`
      const cached = await dragonfly.getJson(cacheKey)
      if (cached) { res.json(cached); return }

      const [totalUnread, openProtocols, totalChats] = await Promise.all([
        chatRepo.countUnreadByInstance(req.tenantId!, instanceId),
        prisma.protocol.count({
          where: {
            tenantId: req.tenantId!,
            status: { in: ['OPEN', 'IN_PROGRESS'] },
          },
        }),
        prisma.chat.count({
          where: {
            tenantId: req.tenantId!,
            instanceId,
            isArchived: false,
            NOT: { jid: { endsWith: '@lid' } }
          },
        }),
      ])

      const stats = { totalUnread, openProtocols, totalChats }
      await dragonfly.setJson(cacheKey, stats, 30)

      res.json(stats)
    } catch (err) {
      handleError(res, err)
    }
  })

  // ═══════════════════════════════════════════════════════════════════════════
  //  GET /api/chats/:id
  // ═══════════════════════════════════════════════════════════════════════════
  router.get('/:id', async (req: Request, res: Response) => {
    try {
      const id = req.params['id'] as string
      const chat = await chatRepo.findById(id, req.tenantId!)
      if (!chat) { res.status(404).json({ error: 'Chat não encontrado' }); return }
      res.json(chat)
    } catch (err) {
      handleError(res, err)
    }
  })

  // ═══════════════════════════════════════════════════════════════════════════
  //  POST /api/chats/:id/read
  // ═══════════════════════════════════════════════════════════════════════════
  router.post('/:id/read', async (req: Request, res: Response) => {
    try {
      const id = req.params['id'] as string
      const tenantId = req.tenantId!
      const chat = await prisma.chat.findFirst({ where: { id, tenantId }, select: { instanceId: true } })
      await chatRepo.clearUnread(id, tenantId)
      if (chat) invalidateChatListCache(tenantId, chat.instanceId).catch(() => {})
      res.json({ ok: true })
    } catch (err) {
      handleError(res, err)
    }
  })

  // ═══════════════════════════════════════════════════════════════════════════
  //  PATCH /api/chats/:id/archive
  // ═══════════════════════════════════════════════════════════════════════════
  router.patch('/:id/archive', async (req: Request, res: Response) => {
    try {
      const id = req.params['id'] as string
      const { archived } = z.object({ archived: z.boolean() }).parse(req.body)
      const tenantId = req.tenantId!
      const chat = await prisma.chat.findFirst({ where: { id, tenantId }, select: { instanceId: true } })
      await chatRepo.setArchived(id, tenantId, archived)
      if (chat) invalidateChatListCache(tenantId, chat.instanceId).catch(() => {})
      res.json({ ok: true })
    } catch (err) {
      handleError(res, err)
    }
  })

  // ═══════════════════════════════════════════════════════════════════════════
  //  PATCH /api/chats/:id/pin
  // ═══════════════════════════════════════════════════════════════════════════
  router.patch('/:id/pin', async (req: Request, res: Response) => {
    try {
      const id = req.params['id'] as string
      const { pinned } = z.object({ pinned: z.boolean() }).parse(req.body)
      const tenantId = req.tenantId!
      const chat = await prisma.chat.findFirst({ where: { id, tenantId }, select: { instanceId: true } })
      await chatRepo.setPinned(id, tenantId, pinned)
      if (chat) invalidateChatListCache(tenantId, chat.instanceId).catch(() => {})
      res.json({ ok: true })
    } catch (err) {
      handleError(res, err)
    }
  })

  // ═══════════════════════════════════════════════════════════════════════════
  //  DELETE /api/chats/:id
  // ═══════════════════════════════════════════════════════════════════════════
  router.delete('/:id', async (req: Request, res: Response) => {
    try {
      const id = req.params['id'] as string
      const chat = await chatRepo.findById(id, req.tenantId!)
      if (!chat) { res.status(404).json({ error: 'Chat não encontrado' }); return }
      await prisma.message.deleteMany({ where: { chatId: id } })
      await prisma.chat.delete({ where: { id } })
      invalidateChatListCache(req.tenantId!, chat.instanceId).catch(() => {})
      res.status(204).send()
    } catch (err) {
      handleError(res, err)
    }
  })

  // ═══════════════════════════════════════════════════════════════════════════
  //  GET /api/chats/:id/messages
  // ═══════════════════════════════════════════════════════════════════════════
  router.get('/:id/messages', async (req: Request, res: Response) => {
    try {
      const id = req.params['id'] as string
      const { before, after, limit } = z.object({
        before: z.string().datetime().optional(),
        after: z.string().datetime().optional(),
        limit: z.coerce.number().int().min(1).max(100).optional(),
      }).parse(req.query)

      const chat = await chatRepo.findById(id, req.tenantId!)
      if (!chat) { res.status(404).json({ error: 'Chat não encontrado' }); return }

      const { messages, hasMore } = await msgRepo.findByChatPaginated(id, {
        before,
        after,
        limit,
      })

      res.json({ messages, hasMore })
    } catch (err) {
      handleError(res, err)
    }
  })

  // ═══════════════════════════════════════════════════════════════════════════
  //  PROTOCOLOS
  // ═══════════════════════════════════════════════════════════════════════════
  router.get('/:id/protocol', async (req: Request, res: Response) => {
    try {
      const id = req.params['id'] as string
      const chat = await chatRepo.findById(id, req.tenantId!)
      if (!chat) { res.status(404).json({ error: 'Chat não encontrado' }); return }

      const protocols = await prisma.protocol.findMany({
        where: { chatId: id },
        orderBy: { createdAt: 'desc' },
        take: 10,
        include: {
          sector: { select: { name: true } },
          team: { select: { name: true } },
        },
      })

      res.json(protocols)
    } catch (err) {
      handleError(res, err)
    }
  })

  router.post('/:id/protocol', async (req: Request, res: Response) => {
    try {
      const id = req.params['id'] as string
      const { sectorId, teamId } = z.object({
        sectorId: z.string().uuid().optional(),
        teamId: z.string().uuid().optional(),
      }).parse(req.body)

      const chat = await chatRepo.findById(id, req.tenantId!)
      if (!chat) { res.status(404).json({ error: 'Chat não encontrado' }); return }
      if (!chat.contactId) { res.status(400).json({ error: 'Chat sem contato associado' }); return }

      const count = await prisma.protocol.count({ where: { chatId: id } })
      const year = new Date().getFullYear()
      const number = `#${String(count + 1).padStart(4, '0')}-${year}`

      const protocol = await prisma.protocol.create({
        data: {
          tenantId: req.tenantId!,
          number,
          chatId: id,
          contactId: chat.contactId,
          sectorId,
          teamId,
          status: 'OPEN',
        },
      })

      invalidateChatListCache(req.tenantId!, chat.instanceId).catch(() => {})
      res.status(201).json(protocol)
    } catch (err) {
      handleError(res, err)
    }
  })

  router.patch('/:chatId/protocol/:protocolId', async (req: Request, res: Response) => {
    try {
      const chatId = req.params['chatId'] as string
      const protocolId = req.params['protocolId'] as string
      const { status, summary } = z.object({
        status: z.enum(['OPEN', 'IN_PROGRESS', 'WAITING_CLIENT', 'WAITING_AGENT', 'RESOLVED', 'CANCELLED']).optional(),
        summary: z.string().max(500).optional(),
      }).parse(req.body)

      const protocol = await prisma.protocol.updateMany({
        where: { id: protocolId, chatId, tenantId: req.tenantId! },
        data: {
          ...(status && { status }),
          ...(summary && { summary }),
          ...(status === 'RESOLVED' && { resolvedAt: new Date() }),
        },
      })

      const chat = await prisma.chat.findFirst({ where: { id: chatId, tenantId: req.tenantId! }, select: { instanceId: true } })
      if (chat) invalidateChatListCache(req.tenantId!, chat.instanceId).catch(() => {})

      res.json({ updated: protocol.count })
    } catch (err) {
      handleError(res, err)
    }
  })

  return router
}
