import type { ExecutionContext } from '@nestjs/common';
import { createParamDecorator } from '@nestjs/common';
import type { AuthenticatedSocket } from '../guards/ws-admin-auth.guard';

/**
 * Extract restaurant ID from authenticated WebSocket client
 * Use this decorator in WebSocket handlers to get the restId from the authenticated admin
 *
 * @example
 * ```typescript
 * @SubscribeMessage('get:notifications')
 * handleGetNotifications(@WsRestId() restId: string) {
 *   // restId is automatically extracted from authenticated admin
 * }
 * ```
 */
export const WsRestId = createParamDecorator((data: unknown, ctx: ExecutionContext): string => {
  const client = ctx.switchToWs().getClient<AuthenticatedSocket>();
  const restId = client.restId;

  if (!restId) {
    throw new Error('Restaurant ID not found. Ensure WsAdminAuthGuard is applied.');
  }

  return restId;
});
