﻿import { NextRequest, NextResponse } from 'next/server';
import { prisma } from '@/lib/prisma';

export async function PUT(
  request: NextRequest,
  { params }: { params: Promise<{ id: string }> | { id: string } }
) {
  try {
    const { id } = await params;
    const body = await request.json();
    
    const workPlan = await prisma.workPlan.update({
      where: { id },
      data: {
        objective: body.objective,
        target: body.target,
        progress: body.progress,
        status: body.status,
        dueDate: body.dueDate ? new Date(body.dueDate) : null,
        linkedTargetId: body.linkedTargetId,
        tasks: body.tasks ? JSON.stringify(body.tasks) : undefined,
      }
    });
    
    return NextResponse.json(workPlan);
  } catch (error) {
    console.error('Error updating work plan:', error);
    return NextResponse.json({ error: 'Failed to update work plan' }, { status: 500 });
  }
}

export async function DELETE(
  request: NextRequest,
  { params }: { params: Promise<{ id: string }> | { id: string } }
) {
  try {
    const { id } = await params;
    await prisma.workPlan.delete({ where: { id } });
    return NextResponse.json({ success: true });
  } catch (error) {
    console.error('Error deleting work plan:', error);
    return NextResponse.json({ error: 'Failed to delete work plan' }, { status: 500 });
  }
}
