//! Connection handles and tenant-scoped room keys for the WebSocket hub.

use athena_chat::ids::{RoomId, UserId};
use tokio::sync::mpsc;
use tokio::task::JoinHandle;
use uuid::Uuid;

use crate::protocol::ServerWsEvent;

/// Composite key isolating room fan-out per tenant (`client_name`) and room id.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct RoomScopeKey {
    pub client_name: String,
    pub room_id: RoomId,
}

impl RoomScopeKey {
    /// Creates a scope key for hub subscription and topic maps.
    pub fn new(client_name: impl Into<String>, room_id: RoomId) -> Self {
        Self {
            client_name: client_name.into(),
            room_id,
        }
    }
}

/// Registered WebSocket session with outbound channel and per-room subscription tasks.
#[derive(Debug)]
pub struct ConnectionHandle {
    pub connection_id: Uuid,
    pub user_id: UserId,
    pub organization_id: String,
    pub client_name: String,
    pub outbound: mpsc::Sender<ServerWsEvent>,
    pub subscriptions: tokio::sync::RwLock<std::collections::HashMap<RoomId, JoinHandle<()>>>,
}
