﻿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 record = await prisma.trainingRecord.update({
      where: { id },
      data: {
        certStatus: body.certStatus,
        certUploadDate: body.certUploadDate ? new Date(body.certUploadDate) : null,
        certFileUrl: body.certFileUrl,
      }
    });
    
    return NextResponse.json(record);
  } catch (error) {
    console.error('Error updating training record:', error);
    return NextResponse.json({ error: 'Failed to update training record' }, { status: 500 });
  }
}

export async function GET(
  request: NextRequest,
  { params }: { params: Promise<{ id: string }> | { id: string } }
) {
  try {
    const { id } = await params;
    
    const record = await prisma.trainingRecord.findUnique({
      where: { id }
    });
    
    if (!record) {
      return NextResponse.json({ error: 'Training record not found' }, { status: 404 });
    }
    
    return NextResponse.json(record);
  } catch (error) {
    console.error('Error fetching training record:', error);
    return NextResponse.json({ error: 'Failed to fetch training record' }, { status: 500 });
  }
}
