#pragma once
#include <event2/event.h>
#include <event2/buffer.h>
#include <tfe_future.h>
#include <MESA/Maat_rule.h>


struct cache_handle;
struct cache_handle* create_web_cache_handle(const char* profile_path, const char* section, 
													struct event_base* gc_evbase, Maat_feather_t feather, void *logger);

struct cached_meta
{
	size_t content_length;
	char* content_type;
	char* last_modified;
	char* etag;
};
const struct cached_meta* cache_query_result_read_meta(future_result_t * result);
size_t cache_query_result_get_data(future_result_t * result, const unsigned char** pp_data);

int web_cache_async_read(struct cache_handle* handle, unsigned int thread_id,
									const struct tfe_http_half * request, struct cache_mid** mid, struct future* f);



enum cache_query_result_type
{
	CACHE_QUERY_RESULT_MISS,
	CACHE_QUERY_RESULT_META,
	CACHE_QUERY_RESULT_DATA,
	CACHE_QUERY_RESULT_END,
	CACHE_QUERY_RESULT_IRRELEVANT,
	__CACHE_QUERY_RESULT_MAX
};
enum cache_query_result_type cache_query_result_get_type(future_result_t * result);

enum cache_pending_result
{
	PENDING_RESULT_NONE=0,
	PENDING_RESULT_FOBIDDEN,
	PENDING_RESULT_REVALIDATE,
	PENDING_RESULT_ALLOWED,
	PENDING_RESULT_HIT,
	PENDING_RESULT_MISS
};
const char* cache_pending_result_string(enum cache_pending_result);
struct cache_mid;
void cache_mid_clear(struct cache_mid **mid);

const struct cached_meta* cache_pending_result_read_meta(future_result_t * result, struct cache_mid* mid);
enum cache_pending_result web_cache_async_pending(struct cache_handle* handle, unsigned int thread_id,
							const struct tfe_http_half * request, struct cache_mid **mid, struct future* f_revalidate);




struct cache_write_context;
struct cache_write_context* web_cache_write_start(struct cache_handle* handle, unsigned int thread_id, 
													const struct tfe_http_session * session, struct cache_mid **mid);
void web_cache_write(struct cache_write_context* ctx, const unsigned char * body_frag, size_t frag_size);
//return 1 on success
//return -1 on failed
//return -2 on cancel

int web_cache_write_end(struct cache_write_context* ctx);



