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

export const GET = withErrorHandler(
  withAuth(async (req: AuthedRequest) => {
    await requireSection(req, 'telephone');
    await requirePlanFeature(req.session.restaurantId!, 'voice_agent');
    await fetchAndSyncTwilioNumbers(req.session.restaurantId!);
    const numbers = await listPhoneNumbers(req.session.restaurantId!);
    return NextResponse.json({ numbers });
  })
);
