/* Page extension: preserves this page’s reviewed document-access scenario. */
static maelys_datalog_result_t *result = NULL;
static maelys_datalog_input_edb_t *edb = NULL;
static maelys_datalog_session_t *session = NULL;
static maelys_datalog_policy_t *policy = NULL;

static void cleanup_context(void) {
    if (result != NULL) {
        cleanup_status(maelys_datalog_result_free(result));
        result = NULL;
    }
    if (edb != NULL) {
        cleanup_status(maelys_datalog_input_edb_free(edb));
        edb = NULL;
    }
    if (session != NULL) {
        cleanup_status(maelys_datalog_session_free(session));
        session = NULL;
    }
    if (policy != NULL) {
        cleanup_status(maelys_datalog_policy_free(policy));
        policy = NULL;
    }
}

/* Page reference context; the document-access case from the guides. */
/* Application-owned setup shared by the reviewed fragments. */

static MAELYS_DATALOG_EXPLANATION_STORAGE(storage, 256u * 1024u);
static const maelys_datalog_value_t terms[] = {
    MAELYS_DATALOG_SYMBOL("alice"), MAELYS_DATALOG_SYMBOL("roadmap.pdf"),
};
static maelys_datalog_status_t prepare(
    maelys_datalog_result_t *result, maelys_datalog_prepared_explanation_t **explanation) {
    return maelys_datalog_result_prepare_explanation(
        result, MAELYS_DATALOG_EXPLAIN_TRUE, "allow", terms, 2u,
        storage, sizeof(storage), explanation);
}
static const maelys_datalog_value_t absent_terms[] = {
    MAELYS_DATALOG_SYMBOL("mallory"), MAELYS_DATALOG_SYMBOL("roadmap.pdf"),
};
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)).";
    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_create(policy, 0u, &session));
    check_status(maelys_datalog_input_edb_create(&edb));
    check_status(MAELYS_DATALOG_ADD_FACT(edb, &diagnostic, "owns", "alice", "roadmap.pdf"));
    /* Intern the absent-query user without granting document ownership. */
    check_status(MAELYS_DATALOG_ADD_FACT(edb, &diagnostic, "user", "mallory"));
    check_status(maelys_datalog_session_solve_edb(session, edb, &result, &diagnostic));
    /* Prepare vocabulary and validate the caller workspace, without acquiring a lease. */
    maelys_datalog_fact_view_t fact;
    size_t count = 0;
    check_status(maelys_datalog_result_enumerate(
        result, "allow", 2u, &fact, 1u, &count));
    if (count != 1u) return EXIT_FAILURE;
    size_t bytes = 0, alignment = 0;
    check_status(maelys_datalog_result_explanation_storage_requirements(
        result, MAELYS_DATALOG_EXPLAIN_TRUE, &bytes, &alignment));
    if (bytes > sizeof(storage) ||
        alignment > _Alignof(max_align_t)) return EXIT_FAILURE;
    {
        /* API_REFERENCE_EXAMPLE */
    }
    cleanup_context();
    if (cleanup_failed) return EXIT_FAILURE;
    return EXIT_SUCCESS;
}
