#pragma once

#include "../shared/protocol/message.h"
#include "auth/authenticator.h"
#include <boost/asio.hpp>
#include <boost/asio/ssl.hpp>
#include <memory>
#include <string>
#include <vector>

namespace scar {

class Server;

class Session : public std::enable_shared_from_this<Session> {
public:
    Session(boost::asio::ssl::stream<boost::asio::ip::tcp::socket> socket,
            std::shared_ptr<Authenticator> auth,
            Server* server);
    
    void start();
    
    // Send message to this session
    void send(const Message& message);
    
    // Get authenticated username (empty if not authenticated)
    const std::string& username() const { return username_; }
    
    // Check if session is authenticated
    bool isAuthenticated() const { return authenticated_; }
    
private:
    void doHandshake();
    void doReadHeader();
    void doReadBody(uint32_t length);
    void handleMessage(std::unique_ptr<Message> message);
    void handleLoginRequest(const LoginRequest& request);
    
    boost::asio::ssl::stream<boost::asio::ip::tcp::socket> socket_;
    std::shared_ptr<Authenticator> auth_;
    Server* server_;
    
    std::vector<uint8_t> read_buffer_;
    std::vector<uint8_t> write_buffer_;
    
    std::string username_;
    bool authenticated_;
};

} // namespace scar
