/* Page extension: preserves this page’s reviewed document-access scenario. */
static maelys_datalog_policy_t *policy = NULL;

static void cleanup_context(void) {
    if (policy != NULL) {
        cleanup_status(maelys_datalog_policy_free(policy));
        policy = NULL;
    }
    (void)remove("documents.dl");
    (void)remove("manifest.json");
}

/* Page reference context; source policy and vocabulary from the public document-access guides. */
static int write_file(const char *path, const char *text) {
    FILE *file = fopen(path, "wb");
    if (file == NULL) return 0;
    size_t length = strlen(text);
    int written = fwrite(text, 1, length, file) == length;
    int closed = fclose(file) == 0;
    return written && closed;
}
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("delegated", 2), MAELYS_DATALOG_EDB("blocked", 1),
        MAELYS_DATALOG_IDB("can_read", 2), MAELYS_DATALOG_IDB_QUERY("has_any_document", 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 = "can_read(User, Doc) :-\n    owns(User, Doc) or delegated(User, Doc),\n    not(blocked(User)).\nhas_any_document(User) :- owns(User, _).\nallow(User, Doc) :- user(User), can_read(User, Doc).\n";
    const char *manifest_json = "{\"policy_set_id\":\"documents\",\"policy_set_version\":\"1.0.0\",\"manifest_version\":\"1\",\"default_profile\":\"enforce\",\"created_for\":\"production\",\"strict_loading\":true,\"fail_closed\":true,\"capabilities\":[],\"policies\":[{\"policy_id\":\"documents.main\",\"domain\":\"documents\",\"file\":\"documents.dl\",\"sha256\":\"be25aa04e2c75e77012c60b1c36c80d5f651371d3887c851d1e0d74975de329a\",\"mode\":\"enforce\",\"enabled\":true,\"description\":\"Document access\",\"queries\":[{\"name\":\"allow\",\"arity\":2},{\"name\":\"has_any_document\",\"arity\":1}]}]}";
    /* The historical text loader accepts MAELYS-DATALOG-v2 as its profile. */
    const char profile_field[] = "\"default_profile\":\"enforce\"";
    const char *profile = strstr(manifest_json, profile_field);
    if (profile == NULL) return EXIT_FAILURE;
    char historical_manifest_json[strlen(manifest_json) + sizeof("MAELYS-DATALOG-v2")];
    int historical_length = snprintf(historical_manifest_json, sizeof(historical_manifest_json),
        "%.*s\"default_profile\":\"MAELYS-DATALOG-v2\"%s",
        (int)(profile - manifest_json), manifest_json, profile + strlen(profile_field));
    if (historical_length < 0 || (size_t)historical_length >= sizeof(historical_manifest_json)) return EXIT_FAILURE;
    if (!write_file("documents.dl", source) || !write_file("manifest.json", manifest_json)) {
        fputs("reference file creation failed\n", stderr);
        return EXIT_FAILURE;
    }
    maelys_datalog_diagnostic_t diagnostic = MAELYS_DATALOG_DIAGNOSTIC_INIT;
    {
        /* API_REFERENCE_EXAMPLE */
    }
    cleanup_context();
    if (cleanup_failed) return EXIT_FAILURE;
    return EXIT_SUCCESS;
}
