#ifndef SUIT_CURATE_H
#define SUIT_CURATE_H
#include "conf.h"
#include <stddef.h>
#include <stdint.h>
// `suit curate` (docs/curate.md): one self-contained HTML page that shows every
// skill, agent and command of the wardrobes beside how it is used, and a text
// file of the choices made on it. It changes nothing else.
constexpr uint32_t CURATE_ASSETS_MAX = 4096; // assets on a page
constexpr uint32_t CURATE_PROJECTS_MAX = 64; // projects listed for one asset; the rest are summed
constexpr uint32_t CURATE_TOP = 10; // names used that match no asset, listed
constexpr size_t CURATE_HEAD_MAX = 16384; // bytes of a file read for its description
constexpr size_t CURATE_DESC_KEEP = 300; // bytes of a description kept
constexpr uint32_t CURATE_FILES_MAX = 4096; // files of one asset counted for its size
constexpr int CURATE_DEPTH_MAX = 16; // folders nested in one asset counted for its size
constexpr size_t CURATE_PAGE_MAX = 16777216; // bytes of the page
constexpr uint32_t CURATE_RECS_MAX = 600000; // (asset, project) uses and unmatched uses held at once
// `suit curate [--since DAYS] [--out FILE]`. ARGV is what follows "curate".
// 0; 1 after a message (a file cannot be read or written, a bound is passed);
// 2 for bad arguments or a config that cannot be used; EXIT_BUSY.
int cmd_curate(int argc, char **argv);
// ---- `suit curate apply` (docs/curate.md, "Applying the choices") -----------------------------
// `suit curate apply FILE [--yes]`. ARGV is what follows "apply". 0 when done, or when nothing
// was to be done, or the plan was shown; 1 when an operation failed; 2 for bad arguments or a
// refused file or config; EXIT_LOCAL_CHANGES (12); EXIT_NEEDS_REPAIR (15); EXIT_BUSY (20).
int cmd_curate_apply(int argc, char **argv);
constexpr size_t CHOICE_PROJECT_MAX = 512; // bytes of a PROJECT in the choices file
constexpr size_t CHOICES_FILE_MAX = 4194304; // bytes of a choices file
typedef enum { CHOICE_KEEP, CHOICE_PERSONAL, CHOICE_RETIRE } ChoiceAct;
typedef struct {
ChoiceAct act;
int kind; // 0 skill, 1 agent, 2 command, as AssetKind
char ward[CONF_NAME_MAX + 1];
char name[CONF_NAME_MAX + 1];
char project[CHOICE_PROJECT_MAX + 1]; // personal only
} CurateChoice;
// What a line of a choices file is: blank or a comment; a choice; malformed (MSG says
// how); or a personal choice whose PROJECT is a folder (starting with ~ or /) or `-`,
// which a config cannot name as a TARGET: refused, MSG says so.
typedef enum { CHOICE_NONE, CHOICE_OK, CHOICE_BAD, CHOICE_FOLDER } ChoiceLine;
// True if the LEN bytes are the first line, `# suit choices 1` (a final CR is allowed).
bool curate_choices_header(const char *line, size_t len);
// Parse one line of LEN bytes, without its newline, into C. MSG (CAP bytes) is set for
// CHOICE_BAD and CHOICE_FOLDER.
ChoiceLine curate_choice_parse(const char *line, size_t len, CurateChoice *c, char *msg, size_t cap);
// The name of the outfit of wardrobe WARD that holds what is limited to PROJECT:
// `personal.WARD.` and PROJECT with every byte outside A-Z a-z 0-9 . _ - replaced by `-`, cut
// to fit CONF_NAME_MAX bytes; for N above 1, with `-N` at the end (the second project that
// would have the same name). OUT has CONF_NAME_MAX + 1 bytes. Returns the length, or 0 when
// `personal.WARD.` and the suffix do not fit in CONF_NAME_MAX bytes (the wardrobe's name is too long).
size_t curate_personal_outfit(const char *ward, const char *project, unsigned n, char *out);
// BASE.WARD (BASE is attic or kept) into OUT (CONF_NAME_MAX + 1 bytes): its length, or 0 if it does not fit.
size_t curate_home_outfit(const char *base, const char *ward, char *out);
// Whether the outfit NAME is an attic: `attic`, or `attic.` and more. Nothing it lists is worn.
bool curate_is_attic(const char *name);
// Edits of a suit.conf as text, in TEXT (LEN bytes, room for CAP), changing only the lines
// named. Lines are counted as the parser counts them. A new line ends as the first line of the
// file does (CR LF or LF).
typedef enum { EDIT_OK, EDIT_LONG, EDIT_FULL } EditResult;
// Rewrite the value of the entry `KEY = VALUE` on LINE with the N WORDS joined by single spaces,
// leaving the key as written and what follows the value (spaces, a comment, a CR) as it was;
// with N 0, remove the line. EDIT_LONG: the line would pass CONF_LINE_MAX bytes. EDIT_FULL: the
// text would pass CAP bytes. Nothing is changed for either.
EditResult curate_edit_value(char *text, size_t *len, size_t cap, int line, const char *const *words, size_t n);
// Insert the line ENTRY after LINE.
EditResult curate_edit_insert(char *text, size_t *len, size_t cap, int line, const char *entry);
// Append, after one blank line (none if the text is empty or already ends in one), the line HEADER and,
// if ENTRY is not null, the line ENTRY.
EditResult curate_edit_append(char *text, size_t *len, size_t cap, const char *header, const char *entry);
// ---- for the tests ----------------------------------------------------------
// The `description` of the front matter in the first LEN bytes of a file, whitespace
// collapsed, into OUT (room for LEN + 1 bytes), and its length: 0 when there is no
// front matter (a first line "---" and a later one) or no description.
size_t curate_front_description(const char *data, size_t len, char *out);
// The length of the first MAX bytes of S (LEN bytes) cut so as not to split a UTF-8 character.
size_t curate_cut(const char *s, size_t len, size_t max);
// A bounded buffer. OVER is set when something did not fit; nothing is then added.
typedef struct {
char *p;
size_t cap;
size_t len;
bool over;
} CurateBuf;
// Append the N bytes of S to B, or set OVER if they do not fit.
void curate_put(CurateBuf *b, const char *s, size_t n);
// Append the LEN bytes of S as a JSON string, quotes included, in which every
// "<", ">", "&", U+2028 and U+2029 is a \u escape and every byte that is not
// UTF-8 is U+FFFD.
void curate_json_string(CurateBuf *b, const char *s, size_t len);
// Call CB for the index of each of the N names (sorted, byte order) that the
// usage name USAGE (LEN bytes) is an invocation of: it is equal to the name, or
// ends with a colon and the name. Returns how many.
uint32_t curate_matches(const char *const *names, uint32_t n, const char *usage, size_t len,
void (*cb)(void *state, uint32_t index), void *state);
// The times, as "YYYY-MM-DDTHH:MM:SS", from which a use counts: in the window of
// DAYS, in the last 90 days, and in each of the last four 7-day periods before NOW.
typedef struct {
char window[20];
char d90[20];
char week[4][20];
} CurateCuts;
void curate_cuts(int64_t now, int days, CurateCuts *c);
// The period of the time T (at least TALLY_TIME_LEN bytes): 0 the oldest to 3 the
// newest, -1 when it is older than 28 days.
int curate_week(const CurateCuts *c, const char *t);
#endif