// socket.js
// Example Socket.io client for AeThex Connect (Supabase JWT auth)

import { io } from 'socket.io-client';
import { createClient } from '@supabase/supabase-js';

// Initialize Supabase client
const supabase = createClient(
  import.meta.env.PUBLIC_SUPABASE_URL,
  import.meta.env.PUBLIC_SUPABASE_ANON_KEY
);

export async function connectSocket() {
  // Get current session (JWT)
  const { data: { session } } = await supabase.auth.getSession();
  if (!session) throw new Error('Not authenticated');

  // Connect to signaling server
  const socket = io(import.meta.env.PUBLIC_SIGNALING_SERVER_URL, {
    auth: { token: session.access_token }
  });

  // Example: handle connection
  socket.on('connect', () => {
    console.log('Connected to signaling server');
  });

  // Example: join a voice room
  function joinVoice(roomId) {
    socket.emit('voice:join', roomId);
  }

  // Example: send/receive offers, answers, ICE
  socket.on('voice:offer', (data) => { /* handle offer */ });
  socket.on('voice:answer', (data) => { /* handle answer */ });
  socket.on('voice:ice-candidate', (data) => { /* handle candidate */ });

  return { socket, joinVoice };
}
