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

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