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

export async function GET() {
  try {
    const service = getMorningSummaryService();
    const webhooks = await service.getWebhooks();
    return NextResponse.json({ webhooks });
  } catch (error) {
    console.error('[GET /api/notifications/morning-summary/webhooks]', error);
    return NextResponse.json({ error: String(error) }, { status: 500 });
  }
}

export async function POST(request: NextRequest) {
  try {
    const body = await request.json();
    const { label, webhook_url } = body as { label: string; webhook_url: string };

    if (!label || !webhook_url) {
      return NextResponse.json({ error: 'label and webhook_url are required' }, { status: 400 });
    }

    const service = getMorningSummaryService();
    const webhook = await service.createWebhook(label, webhook_url);
    return NextResponse.json({ webhook }, { status: 201 });
  } catch (error) {
    console.error('[POST /api/notifications/morning-summary/webhooks]', error);
    return NextResponse.json({ error: String(error) }, { status: 500 });
  }
}
