package com.qaup.collision.websocket.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.web.socket.config.annotation.EnableWebSocket;
import org.springframework.web.socket.config.annotation.WebSocketConfigurer;
import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry;

import com.qaup.collision.websocket.handler.CollisionWebSocketHandler;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * WebSocket配置类
 * 基于Spring WebSocket框架，负责注册WebSocket处理器
 * 
 * @author qaup-collision
 */
@Configuration
@EnableWebSocket
public class WebSocketConfig implements WebSocketConfigurer {

    private static final Logger LOGGER = LoggerFactory.getLogger(WebSocketConfig.class);
    
    private final CollisionWebSocketHandler collisionWebSocketHandler;

    public WebSocketConfig(CollisionWebSocketHandler collisionWebSocketHandler) {
        this.collisionWebSocketHandler = collisionWebSocketHandler;
        LOGGER.info("🚀 WebSocket配置类初始化...");
    }

    @Override
    public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
        LOGGER.info("📝 注册WebSocket处理器...");
        
        // 注册冲突检测WebSocket端点
        registry.addHandler(collisionWebSocketHandler, "/collision")
                .setAllowedOrigins("*"); // 允许所有来源，生产环境应该限制具体域名
        
        LOGGER.info("✅ WebSocket端点注册完成");    
        LOGGER.info("🎯 端点路径: /collision");
        LOGGER.info("🌐 允许的来源: *");
        LOGGER.info("📡 WebSocket服务可用: ws://localhost:8080/collision");
    }
} 