// ============================================================================
// LUA GAME WRAPPER - HEADER
// ============================================================================
// Allows Lua scripts to implement the Game interface

#ifndef LUA_GAME_H
#define LUA_GAME_H

#include "game.h"
#include <string>

extern "C" {
    #include "lua.h"
    #include "lualib.h"
    #include "lauxlib.h"
}

/**
 * @brief Game implementation that runs Lua scripts
 * 
 * Loads a .lua file from SD card and calls its init(), update(), and draw()
 * functions. The Lua script has access to drawing primitives, input events,
 * and persistent state variables.
 */
class LuaGame : public Game {
public:
    /**
     * @brief Construct a new Lua Game
     * @param script_path Path to .lua file on SD card (e.g., "/games/pong.lua")
     * @param width Display width in pixels
     * @param height Display height in pixels
     * @param renderer Pointer to low-level rendering interface
     * @param gui Pointer to GUI drawing primitives
     * @param input_manager Pointer to input manager
     */
    LuaGame(const char* script_path, uint16_t width, uint16_t height, 
            LowLevelRenderer* renderer, LowLevelGUI* gui, InputManager* input_manager);
    
    /**
     * @brief Destructor - clean up Lua state
     */
    ~LuaGame() override;
    
    /**
     * @brief Initialize game - calls Lua init() function
     */
    void init() override;
    
    /**
     * @brief Update game logic - calls Lua update(event) function
     * @param event Input event from InputManager
     * @return true if screen redraw is needed
     */
    bool update(const InputEvent& event) override;
    
    /**
     * @brief Draw the game - calls Lua draw() function
     */
    void draw() override;
    
    /**
     * @brief Check if game wants to exit
     * @return true if Lua script requested exit
     */
    bool wants_to_exit() const override;
    
    /**
     * @brief Check if game wants continuous frame updates
     * @return true if Lua script set __wants_frame_updates flag
     */
    bool wants_frame_updates() const override;
    
    /**
     * @brief Get Lua state for bindings access
     */
    lua_State* get_lua_state() { return L; }
    
    /**
     * @brief Check if Lua script loaded successfully
     */
    bool is_loaded() const { return loaded; }
    
    /**
     * @brief Get last error message if load failed
     */
    const char* get_error() const { return error_message.c_str(); }

private:
    lua_State* L;
    std::string script_path;
    std::string error_message;
    bool loaded;
    
    /**
     * @brief Load and execute Lua script file from SD card
     * @return true if successful, false on error
     */
    bool load_script();
    
    /**
     * @brief Call a Lua function safely with error handling
     * @param func_name Name of Lua function to call
     * @param nargs Number of arguments on stack
     * @param nresults Number of expected return values
     * @return true if successful, false on error
     */
    bool call_lua_function(const char* func_name, int nargs = 0, int nresults = 0);
    
    /**
     * @brief Report Lua error to console and store message
     */
    void report_error(const char* context);
};

#endif // LUA_GAME_H
