/**
 * POST /api/telephone/sip/:id/refresh
 *
 * Pulls live registration state from jambonz and updates
 * sip_config.registration_status + last_checked_at on the SIP line. Surface
 * for the SIP detail/list UI to keep the registration pill in sync without
 * waiting for a passive poller.
 */
import { NextResponse } from 'next/server';
import { withErrorHandler, RouteContext } from '@server/middleware/withErrorHandler';
import { withAuth, requireSection, AuthedRequest } from '@server/middleware/withAuth';
import { withSipFeatureAuthed } from '@server/middleware/withSipFeature';
import { refreshSipLineStatus } from '@server/services/telephone/sip/sip.service';

export const POST = withErrorHandler(
  withAuth(
    withSipFeatureAuthed(async (req: AuthedRequest, ctx: RouteContext) => {
      await requireSection(req, 'telephone', 'update');
      const { id } = await ctx.params;
      const line = await refreshSipLineStatus(id, req.session.restaurantId!);
      return NextResponse.json({ line });
    })
  )
);
