/**
 * Auto-generated by ${VOCAB.factory}
 * WorkflowExecution Dynamic API Routes
 */

import { NextRequest, NextResponse } from 'next/server';
import { prisma } from '@/lib/db';
import { VOCAB } from '@/lib/white-label/constants';

export const dynamic = 'force-dynamic';

interface RouteContext {
  params: Promise<{ id: string }>;
}


// GET - Get a single WorkflowExecution by ID
export async function GET(
  request: NextRequest,
  context: RouteContext
) {
  try {
    const { id } = await context.params;

    const workflowExecution = await prisma.workflowExecution.findUnique({
      where: { id },
    });

    if (!workflowExecution) {
      return NextResponse.json(
        { error: 'WorkflowExecution not found' },
        { status: 404 }
      );
    }

    return NextResponse.json(workflowExecution);
  } catch (error) {
    console.error('Error fetching WorkflowExecution:', error);
    return NextResponse.json(
      { error: 'Failed to fetch WorkflowExecution' },
      { status: 500 }
    );
  }
}


// PUT - Update a WorkflowExecution
export async function PUT(
  request: NextRequest,
  context: RouteContext
) {
  try {
    const { id } = await context.params;
    const body = await request.json();

    // Remove fields that shouldn't be updated
    delete body.id;
    delete body.createdAt;
    delete body.updatedAt;

    const workflowExecution = await prisma.workflowExecution.update({
      where: { id },
      data: body,
    });

    return NextResponse.json(workflowExecution);
  } catch (error) {
    console.error('Error updating WorkflowExecution:', error);
    return NextResponse.json(
      { error: 'Failed to update WorkflowExecution' },
      { status: 500 }
    );
  }
}


// DELETE - Delete a WorkflowExecution
export async function DELETE(
  request: NextRequest,
  context: RouteContext
) {
  try {
    const { id } = await context.params;

    await prisma.workflowExecution.delete({
      where: { id },
    });

    return NextResponse.json({ success: true });
  } catch (error) {
    console.error('Error deleting WorkflowExecution:', error);
    return NextResponse.json(
      { error: 'Failed to delete WorkflowExecution' },
      { status: 500 }
    );
  }
}
