﻿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 target = await prisma.quarterlyTarget.update({
      where: { id },
      data: {
        title: body.title,
        quarter: body.quarter,
        year: body.year,
        target: body.target ?? body.kpiMeasurement,
        kpiMeasurement: body.kpiMeasurement,
        expectedOutcome: body.expectedOutcome,
        completionTimeline: body.completionTimeline,
        weight: body.weight,
        progress: body.progress,
        status: body.status,
        supervisorComments: body.supervisorComments,
        score: body.score,
        linkedWorkPlanId: body.linkedWorkPlanId,
        updatedAt: new Date(),
      },
    });

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

export async function DELETE(
  request: NextRequest,
  { params }: { params: Promise<{ id: string }> | { id: string } }
) {
  try {
    const { id } = await params;
    await prisma.quarterlyTarget.delete({ where: { id } });
    return NextResponse.json({ message: 'Quarterly target deleted successfully' });
  } catch (error) {
    console.error('Error deleting quarterly target:', error);
    return NextResponse.json({ error: 'Failed to delete quarterly target' }, { status: 500 });
  }
}
