import { NextResponse } from 'next/server';
import { withErrorHandler } from '@server/middleware/withErrorHandler';
import { withAuth, requireSection, AuthedRequest } from '@server/middleware/withAuth';
import { deletePhoneNumber } from '@server/services/telephone/twilio-phone.service';
import { requirePlanFeature } from '@server/utils/features';

export const DELETE = withErrorHandler(
  withAuth(async (req: AuthedRequest, { params }: { params: Promise<{ id: string }> }) => {
    await requireSection(req, 'telephone', 'delete');
    await requirePlanFeature(req.session.restaurantId!, 'voice_agent');
    const { id } = await params;
    await deletePhoneNumber(req.session.restaurantId!, id);
    return NextResponse.json({ success: true });
  })
);
