// SPDX-License-Identifier: GPL-2.0-only
/*
 *  ppm/include/ppm/registry.h
 *
 *  Copyright (C) 2026 Project Tick
 */

#ifndef PPM_REGISTRY_H
#define PPM_REGISTRY_H

#include <stddef.h>

#include <ppm/error.h>
#include <ppm/manifest.h>

#ifdef __cplusplus
extern "C" {
#endif

/*
 * Registry — a git repository whose working tree is a package index.
 *
 * Each commit is an immutable snapshot. ppm pins the registry to a specific
 * commit per lockfile; updates explicitly fast-forward the local mirror to
 * a newer (signed) commit.
 *
 * The on-disk layout is documented in AGENTS.md.
 */

typedef struct ppm_registry ppm_registry_t;

ppm_error_t ppm_registry_open  (const char *local_mirror, ppm_registry_t **out);
ppm_error_t ppm_registry_clone (const char *url, const char *local_mirror,
                                ppm_registry_t **out);
ppm_error_t ppm_registry_fetch (ppm_registry_t *reg);
void        ppm_registry_close (ppm_registry_t *reg);

/* The SHA-1 of the currently checked-out commit. Caller frees. */
ppm_error_t ppm_registry_head_commit(const ppm_registry_t *reg, char **out_hex);

/* List every published version of a package, newest first. */
ppm_error_t ppm_registry_versions(const ppm_registry_t *reg,
                                  const char *name,
                                  ppm_version_t **out, size_t *out_len);

/* Load the immutable manifest for a specific package version. */
ppm_error_t ppm_registry_manifest(const ppm_registry_t *reg,
                                  const char *name,
                                  const ppm_version_t *version,
                                  ppm_manifest_t **out);

/* Substring search across package names. Caller frees the array. */
ppm_error_t ppm_registry_search(const ppm_registry_t *reg,
                                const char *query,
                                char ***out_names, size_t *out_len);

#ifdef __cplusplus
}
#endif

#endif /* PPM_REGISTRY_H */
