import { NextResponse } from 'next/server';
import { withErrorHandler, RouteContext } from '@server/middleware/withErrorHandler';
import { withAuth, requireSection, AuthedRequest } from '@server/middleware/withAuth';
import { getCallSession } from '@server/services/call-sessions.service';
import { NotFoundError, ValidationError } from '@server/errors';
import { requirePlanFeature } from '@server/utils/features';

export const GET = withErrorHandler(
  withAuth(async (req: AuthedRequest, context: RouteContext) => {
    const { restaurantId } = req.session;
    if (!restaurantId) throw new ValidationError('Restaurant context required');
    await requireSection(req, 'telephone');
    await requirePlanFeature(restaurantId, 'voice_agent');

    const { id } = await context.params;
    const session = await getCallSession(id, restaurantId);
    if (!session) throw new NotFoundError('Call session');
    return NextResponse.json({ session });
  })
);
