import { NextRequest, NextResponse } from 'next/server';
import { getMorningSummaryService } from '@/lib/services/morning-summary-service';

export async function PUT(request: NextRequest, { params }: { params: Promise<{ id: string }> }) {
  try {
    const { id: rawId } = await params;
    const id = parseInt(rawId, 10);
    if (isNaN(id)) return NextResponse.json({ error: 'Invalid id' }, { status: 400 });

    const body = await request.json();
    const service = getMorningSummaryService();
    const webhook = await service.updateWebhook(id, body);
    return NextResponse.json({ webhook });
  } catch (error) {
    console.error('[PUT /api/notifications/morning-summary/webhooks/:id]', error);
    return NextResponse.json({ error: String(error) }, { status: 500 });
  }
}

export async function DELETE(_req: NextRequest, { params }: { params: Promise<{ id: string }> }) {
  try {
    const { id: rawId } = await params;
    const id = parseInt(rawId, 10);
    if (isNaN(id)) return NextResponse.json({ error: 'Invalid id' }, { status: 400 });

    const service = getMorningSummaryService();
    await service.deleteWebhook(id);
    return NextResponse.json({ deleted: true });
  } catch (error) {
    console.error('[DELETE /api/notifications/morning-summary/webhooks/:id]', error);
    return NextResponse.json({ error: String(error) }, { status: 500 });
  }
}
