﻿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 kpi = await prisma.kPI.update({
      where: { id },
      data: {
        title: body.title,
        description: body.description,
        targetValue: body.targetValue,
        measurementMethod: body.measurementMethod,
        frequency: body.frequency,
        performanceScore: body.performanceScore,
        progress: body.progress,
        currentValue: body.currentValue,
        targetDeadline: body.targetDeadline ? new Date(body.targetDeadline) : undefined,
        linkedWorkPlanId: body.linkedWorkPlanId,
        linkedGoalId: body.linkedGoalId,
        updatedAt: new Date(),
      },
    });

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

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