import { NextRequest, NextResponse } from 'next/server';

// Mock data - in real app, use database
const mockCertificates = [
  {
    id: '1',
    recipientName: 'Andi Wijaya',
    recipientEmail: 'andi.wijaya@trainee.dapurmbg.com',
    courseName: 'Teknik Dasar Memasak',
    completionDate: '2024-01-15',
    certificateNumber: 'MBG-TDM-2024-001',
    score: 92,
    status: 'issued',
    issuedBy: 'Chef Ahmad Fauzi',
    templateId: 'template-1',
    notes: 'Peserta menunjukkan kemampuan memasak dasar yang sangat baik.'
  },
  // Add more as needed
];

export async function POST(
  request: NextRequest,
  { params }: { params: { id: string } }
) {
  try {
    const { id } = params;
    const index = mockCertificates.findIndex(cert => cert.id === id);

    if (index === -1) {
      return NextResponse.json(
        { error: 'Sertifikat tidak ditemukan' },
        { status: 404 }
      );
    }

    if (mockCertificates[index].status !== 'issued') {
      return NextResponse.json(
        { error: 'Sertifikat tidak dapat dicabut' },
        { status: 400 }
      );
    }

    // Update status
    mockCertificates[index].status = 'revoked';

    // In real app, save to database and perhaps log or notify

    return NextResponse.json({
      success: true,
      message: 'Sertifikat berhasil dicabut',
      data: {
        id,
        newStatus: 'revoked',
        revokedAt: new Date().toISOString(),
      }
    });

  } catch (error) {
    console.error('Error revoking certificate:', error);
    return NextResponse.json(
      { error: 'Gagal mencabut sertifikat' },
      { status: 500 }
    );
  }
}