import { NextRequest, NextResponse } from 'next/server'; import { query } from '@/lib/db'; export async function GET(req: NextRequest) { const { searchParams } = new URL(req.url); const slug = searchParams.get('slug'); if (!slug) { return NextResponse.json({ error: 'Missing tenant slug' }, { status: 400 }); } try { const tenantRes = await query('SELECT id FROM tenants WHERE slug = $1', [slug]); if (tenantRes.rows.length === 0) return NextResponse.json([]); const tenantId = tenantRes.rows[0].id; // Fetch notifications const notifsRes = await query(` SELECT id, type, title, message, is_read as read, created_at FROM notifications WHERE tenant_id = $1 ORDER BY created_at DESC LIMIT 20 `, [tenantId]); // Format relative time helper const formatted = notifsRes.rows.map(n => { const diffMs = new Date().getTime() - new Date(n.created_at).getTime(); const diffMins = Math.floor(diffMs / 60000); const diffHours = Math.floor(diffMins / 60); const diffDays = Math.floor(diffHours / 24); let timeStr = 'Agora mesmo'; if (diffDays > 0) { timeStr = `${diffDays} dia${diffDays > 1 ? 's' : ''} atrás`; } else if (diffHours > 0) { timeStr = `${diffHours} hora${diffHours > 1 ? 's' : ''} atrás`; } else if (diffMins > 0) { timeStr = `${diffMins} min${diffMins > 1 ? 's' : ''} atrás`; } return { id: n.id, type: n.type, title: n.title, message: n.message, read: n.read, time: timeStr }; }); return NextResponse.json(formatted); } catch (error: any) { console.error('GET Notifications error:', error); return NextResponse.json({ error: error.message }, { status: 500 }); } } export async function PUT(req: NextRequest) { try { const { slug } = await req.json(); if (!slug) return NextResponse.json({ error: 'Missing tenant slug' }, { status: 400 }); const tenantRes = await query('SELECT id FROM tenants WHERE slug = $1', [slug]); if (tenantRes.rows.length === 0) return NextResponse.json({ error: 'Tenant not found' }, { status: 404 }); const tenantId = tenantRes.rows[0].id; // Mark all as read await query('UPDATE notifications SET is_read = true WHERE tenant_id = $1', [tenantId]); return NextResponse.json({ success: true }); } catch (error: any) { console.error('PUT Notifications error:', error); return NextResponse.json({ error: error.message }, { status: 500 }); } } export async function DELETE(req: NextRequest) { const { searchParams } = new URL(req.url); const slug = searchParams.get('slug'); if (!slug) { return NextResponse.json({ error: 'Missing tenant slug' }, { status: 400 }); } try { const tenantRes = await query('SELECT id FROM tenants WHERE slug = $1', [slug]); if (tenantRes.rows.length === 0) return NextResponse.json({ error: 'Tenant not found' }, { status: 404 }); const tenantId = tenantRes.rows[0].id; // Delete all notifications for this tenant await query('DELETE FROM notifications WHERE tenant_id = $1', [tenantId]); return NextResponse.json({ success: true }); } catch (error: any) { console.error('DELETE Notifications error:', error); return NextResponse.json({ error: error.message }, { status: 500 }); } }