/**
 * Letzte 100 Trigger-Logs eines Templates — fürs Debug-/Audit-UI.
 *   GET /api/trigger-templates/{id}/logs
 */

import { NextRequest, NextResponse } from 'next/server';
import { getRecentTriggerLogs, getTriggerTemplateById } from '@/lib/db/trigger-template-repo';
import { withAuth } from '@/core/auth';

export const runtime = 'nodejs';
export const dynamic = 'force-dynamic';

export const GET = withAuth<{ params: Promise<{ id: string }> }>(async (req: NextRequest, _ctx, ctx: { params: Promise<{ id: string }> }) => {
  const { id } = await ctx.params;
  const t = getTriggerTemplateById(id);
  if (!t) return NextResponse.json({ error: 'Template not found' }, { status: 404 });

  const logs = getRecentTriggerLogs(id, 100);
  return NextResponse.json({ logs });
});
