/**
 * Auto-generated by ${VOCAB.factory}
 * Task 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 Task by ID
export async function GET(
  request: NextRequest,
  context: RouteContext
) {
  try {
    const { id } = await context.params;

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

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

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


// PUT - Update a Task
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 task = await prisma.task.update({
      where: { id },
      data: body,
    });

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


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

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

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