#pragma once

#include "session.h"
#include "config/server_config.h"
#include "database/database.h"
#include "auth/authenticator.h"
#include <boost/asio.hpp>
#include <boost/asio/ssl.hpp>
#include <memory>
#include <set>
#include <mutex>

namespace scar {

class Server {
public:
    Server(const ServerConfig& config);
    
    void run();
    void stop();
    
    // Session management
    void addSession(std::shared_ptr<Session> session);
    void removeSession(std::shared_ptr<Session> session);
    
    // Broadcasting
    void broadcastMessage(const TextMessage& message);
    void broadcastToOthers(const Message& message, std::shared_ptr<Session> exclude);
    
private:
    void doAccept();
    
    boost::asio::io_context io_context_;
    boost::asio::ssl::context ssl_context_;
    boost::asio::ip::tcp::acceptor acceptor_;
    
    std::shared_ptr<Database> database_;
    std::shared_ptr<Authenticator> authenticator_;
    
    std::set<std::shared_ptr<Session>> sessions_;
    std::mutex sessions_mutex_;
    
    ServerConfig config_;
};

} // namespace scar
