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

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

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