export const dynamic = 'force-dynamic';

import { NextRequest, NextResponse } from 'next/server';
import { prisma } from '@/lib/db';
import { requireDFAuth } from '@/lib/dark-factory/auth-guard';

// GET — list all workflows with steps
export async function GET(req: NextRequest) {
  const authResult = await requireDFAuth();
  if (!authResult.authorized) return authResult.response;

  const workflows = await prisma.workflow.findMany({
    orderBy: { createdAt: 'desc' },
    include: {
      workflowSteps: { orderBy: { order: 'asc' } },
      _count: { select: { WorkflowExecution: true } },
    },
  });

  return NextResponse.json({ workflows });
}
