import { APPWRITE_ALMEIRA_CONFIG } from '@/lib/appwrite'; import { createAdminClient } from '@/lib/appwrite.server'; import { Query } from 'node-appwrite'; import Link from 'next/link'; import { MessageCircle } from 'lucide-react'; import { Settings } from '@/types/settings'; import LiveClock from './LiveClock'; export default async function NewsTicker() { let tickerItems: any[] = []; let showNewsDate = true; try { const client: any = await createAdminClient(); if (!client) throw new Error("No Appwrite client available"); // 1. Fetch Settings const settingsRes = await client.databases.listDocuments(APPWRITE_ALMEIRA_CONFIG.databaseId, 'settings', []); const settings = (settingsRes.documents.length > 0 ? settingsRes.documents[0] : { ticker_count: 10, rotation_seconds: 5, feed_news_count: 12, show_news_date: true }) as Settings; showNewsDate = settings.show_news_date !== false; const tickerCount = settings.ticker_count || 10; // 2. Fetch News (NOTICIA) const typeRes = await client.databases.listDocuments(APPWRITE_ALMEIRA_CONFIG.databaseId, 'publication_types', [ Query.equal('codigo', 'NOTICIA') ]); if (typeRes.documents.length > 0) { const firstType = typeRes.documents[0]; const typeId = firstType?.$id; if (!typeId) throw new Error("Could not find typeId for NOTICIA"); const postsRes = await client.databases.listDocuments(APPWRITE_ALMEIRA_CONFIG.databaseId, 'posts', [ Query.equal('tipo_publicacion_id', typeId), Query.equal('estado', ['Publicado', 'publicado']), Query.orderDesc('fecha_publicacion'), Query.limit(tickerCount) ]); // Resolve Media const mediaIds = postsRes.documents.map((p: any) => p.imagen_destacada_id).filter((id: any) => id && typeof id === 'string') as string[]; const mediaMap = new Map(); if (mediaIds.length > 0) { const mediaRes = await client.databases.listDocuments(APPWRITE_ALMEIRA_CONFIG.databaseId, 'media', [ Query.equal('$id', mediaIds) ]); mediaRes.documents.forEach((m: any) => mediaMap.set(m.$id, m)); } postsRes.documents.forEach((post: any) => { const media = typeof post.imagen_destacada_id === 'object' ? post.imagen_destacada_id : mediaMap.get(post.imagen_destacada_id); let imagenUrl = post.og_image_url || null; if (media) { imagenUrl = `${APPWRITE_ALMEIRA_CONFIG.endpoint}/storage/buckets/${media.bucket_id}/files/${media.file_id}/view?project=${APPWRITE_ALMEIRA_CONFIG.projectId}`; } tickerItems.push({ type: 'news', id: post.$id, titulo: post.titulo, slug: post.slug || post.$id, imagenUrl }); }); } // 3. Fetch Approved WhatsApp Messages try { let waRes; try { waRes = await client.databases.listDocuments(APPWRITE_ALMEIRA_CONFIG.databaseId, 'whatsapp_messages', [ Query.equal('status', 'approved'), Query.orderDesc('timestamp'), Query.limit(5) ]); } catch (queryError: any) { // El atributo 'status' puede no estar todavía en el esquema de Appwrite (Modo Gracia) // Usamos fallback de todos los mensajes limitados si falla el filtro waRes = await client.databases.listDocuments(APPWRITE_ALMEIRA_CONFIG.databaseId, 'whatsapp_messages', [ Query.orderDesc('$createdAt'), Query.limit(5) ]); } if (waRes && waRes.documents) { waRes.documents.forEach((msg: any) => { tickerItems.push({ type: 'whatsapp', id: msg.$id, titulo: `${msg.sender_name || 'Usuario'}: ${msg.content || ''}`, slug: null, imagenUrl: msg.avatar || null }); }); } } catch (waError) { console.warn("Skipping WhatsApp messages in ticker (Collection or Schema missing):", waError); } } catch (e) { console.error("Ticker fetch error", e); } if (tickerItems.length === 0) return null; return (