import { GenerateKeysService } from '@/modules/admins/services/generate-key.service';
import { Socket } from 'socket.io';

export async function clientGetWayMiddleware(
  client: Socket,
  generateKeysService: GenerateKeysService,
): Promise<void> {
  const client_key = client.handshake.headers.authorization;

  if (!client_key) {
    client.disconnect();
    return;
  }

  const key = await generateKeysService.generateKeyRepo.findOne({
    where: { client_key },
  });

  if (!key) {
    client.disconnect();
    return;
  }

  console.log(`✅ WebSocket authenticated: Client ID ${key.client_key}`);
}
