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

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

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

  return adminId;
});
