#pragma once
#include <stddef.h>

#include <event2/buffer.h>

#include "pattern_replace.h"
#include <tfe_http.h>
#include <MESA/elua.h>

struct tsg_lua_script
{
	int lua_is_cache;
	struct elua_vm **http_lua_handle;
    int (*http_lua_profile)(int profile_id, struct elua_script ***elua_ctx);
};

struct tsg_script_ctx
{
    int config_id;
	int profile_id;
    int http_req_uri;
    int rewrite_header;
    char *rewrite_uri;
    int execut_lua_sucess;
    struct elua_context *elua_ctx;
    enum tfe_http_event events;
    void * local_logger;
    struct tfe_stream_addr * addr;
    const struct tfe_http_session *session;
	struct tfe_http_half *replacing;
	struct evbuffer *http_body;
	struct evbuffer *http_lua_body;
	int actually_executed;
};

struct elua_context * http_lua_ctx_new(struct tsg_lua_script *lua_script, unsigned int thread_id);
void http_lua_ctx_free(struct tsg_lua_script *lua_script, unsigned int thread_id, struct elua_context * lua_ctx);
struct elua_script *http_lua_map_cache_script(struct elua_vm *vm, const char *script, size_t script_len, size_t timeout_ms);

size_t execute_lua_script_rule(struct tsg_lua_script *lua_script, int profile_id, struct elua_context * lua_ctx, unsigned int thread_id, void *user_data);
int http_lua_handle_create(struct tsg_lua_script *lua_script, int thread_num, const char *name_space);

