/* Published application resources; the context explicitly selects the installed reference ABI 6 backend. */
#include <maelys/datalog_resources.h>
#include <maelys/datalog_extension.h>
#include <stdint.h>

static maelys_datalog_policy_t *policy = NULL, *caller_policy = NULL;
static maelys_datalog_session_t *session = NULL;
static maelys_datalog_session_config_t *config = NULL;
static void *policy_storage = NULL, *session_storage = NULL;
static void cleanup_context(void) {
    if (session != NULL) { cleanup_status(maelys_datalog_session_free(session)); session = NULL; }
    if (config != NULL) { cleanup_status(maelys_datalog_session_config_free(config)); config = NULL; }
    if (caller_policy != NULL) { cleanup_status(maelys_datalog_policy_free(caller_policy)); caller_policy = NULL; }
    if (policy != NULL) { cleanup_status(maelys_datalog_policy_free(policy)); policy = NULL; }
    free(session_storage); session_storage = NULL;
    free(policy_storage); policy_storage = NULL;
}
static void *allocate_storage(size_t bytes, size_t alignment) {
    void *memory = malloc(bytes);
    if (memory == NULL || alignment == 0 || (uintptr_t)memory % alignment != 0) {
        free(memory); fputs("Storage allocation failed\n", stderr); exit(EXIT_FAILURE);
    }
    return memory;
}
static void create_session(void) {
    check_status(maelys_datalog_session_create_configured(policy, 0, config, &session));
}
static int allocation_stats_unchanged(
    const maelys_datalog_session_allocation_stats_t *a,
    const maelys_datalog_session_allocation_stats_t *b) {
    return a->struct_size == b->struct_size
        && a->contract_version == b->contract_version
        && a->reserved == b->reserved
        && a->required_features == b->required_features
        && a->cap_bytes == b->cap_bytes
        && a->current_bytes == b->current_bytes
        && a->remaining_bytes == b->remaining_bytes
        && a->operation_peak_bytes == b->operation_peak_bytes;
}
int main(void) {
    if (atexit(cleanup_context) != 0) return EXIT_FAILURE;
    const maelys_datalog_predicate_t predicates[] = {
        MAELYS_DATALOG_EDB("user", 1), MAELYS_DATALOG_EDB("owns", 2),
        MAELYS_DATALOG_EDB("blocked", 1), MAELYS_DATALOG_IDB_QUERY("allow", 2),
    };
    const maelys_datalog_domain_t domain = MAELYS_DATALOG_DOMAIN_NO_ATOMS("documents", predicates);
    check_status(maelys_datalog_domain_register(&domain));
    const char *source = "allow(User, Doc) :- owns(User, Doc), not(blocked(User)).";
    const char *manifest_json = "{\"policy_set_id\":\"documents\",\"policy_set_version\":\"1.0.0\",\"manifest_version\":\"1\",\"default_profile\":\"MAELYS-DATALOG-v2\",\"created_for\":\"production\",\"strict_loading\":true,\"fail_closed\":true,\"capabilities\":[],\"policies\":[{\"policy_id\":\"documents.main\",\"domain\":\"documents\",\"file\":\"documents.dl\",\"sha256\":\"c5ec08d836b313af6e49ab7c49e811d84e0e856884fab645282d74682ceac454\",\"mode\":\"enforce\",\"enabled\":true,\"description\":\"Document access\",\"queries\":[{\"name\":\"allow\",\"arity\":2}]}]}";
    const maelys_datalog_policy_bundle_entry_t bundle[] = {
        { .policy_id = "documents.main", .src = source, .src_len = strlen(source) },
    };
    maelys_datalog_diagnostic_t diagnostic = MAELYS_DATALOG_DIAGNOSTIC_INIT;
    check_status(maelys_datalog_policy_load_inline("documents", "reference-context", source,
        strlen(source), &policy, &diagnostic));
    check_status(maelys_datalog_session_config_create(&config));
    check_status(maelys_datalog_session_config_set_backend_v6(config, maelys_datalog_backend_reference_v6()));
    {
        /* API_REFERENCE_EXAMPLE */
    }
    cleanup_context();
    if (cleanup_failed) return EXIT_FAILURE;
    return EXIT_SUCCESS;
}
