package com.snack.server.config;

import com.snack.server.module.chat.websocket.ChatHandshakeInterceptor;
import com.snack.server.module.chat.websocket.ChatWebSocketHandler;
import lombok.RequiredArgsConstructor;
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;

/**
 * WebSocket 配置（原生 WebSocket + JSON 文本协议）
 *
 * 端点：/ws/chat?token=xxx&type=user|admin
 * 客户端使用 VueUse useWebSocket（浏览器原生 WebSocket）直连
 */
@Configuration
@EnableWebSocket
@RequiredArgsConstructor
public class WebSocketConfig implements WebSocketConfigurer {

    private final ChatWebSocketHandler chatWebSocketHandler;

    @Override
    public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
        registry.addHandler(chatWebSocketHandler, "/ws/chat")
                .addInterceptors(new ChatHandshakeInterceptor())
                .setAllowedOriginPatterns("*");
    }
}
