package socket

import (
	"context"

	protoSocket "git.toki-labs.com/toki/proto-socket/go"
	"nhooyr.io/websocket"

	"git.toki-labs.com/toki/alt/services/worker/internal/config"
	workerContracts "git.toki-labs.com/toki/alt/services/worker/internal/contracts"
)

const (
	serverName                = "alt-worker"
	serverVersion             = "dev"
	defaultAltProtocolVersion = "alt.v1"
)

type Server struct {
	wsServer *protoSocket.WsServer
}

func NewServer(cfg config.Config, deps Deps) *Server {
	wsServer := protoSocket.NewWsServer(cfg.Host, cfg.Port, cfg.SocketPath, func(conn *websocket.Conn) *protoSocket.WsClient {
		return protoSocket.NewWsClient(conn, 30, 10, workerContracts.ParserMap())
	})
	wsServer.OnClientConnected = registerSessionHandlers(deps)
	return &Server{
		wsServer: wsServer,
	}
}

func (s *Server) Start(ctx context.Context) error {
	return s.wsServer.Start(ctx)
}

func (s *Server) Stop() error {
	return s.wsServer.Stop()
}
