#pragma once

#include <stdbool.h>
#include <stddef.h>

/*
 * Persistent terminal session state.
 *
 * Sessions are stored as JSON files under $XDG_DATA_HOME/foot/state/<name>.json
 * (defaulting to ~/.local/share/foot/state). Each file records the cwd of the
 * foreground process on the pty plus its argv, so a session can be resurrected
 * in a new tab.
 */

struct session_state {
    char *cwd;
    int argc;
    char **argv;  /* NULL-terminated for convenience; argc does not count the terminator */
};

void session_state_free(struct session_state *st);

/*
 * Inspect the pty for its foreground process. On Linux, walks /proc to grab
 * cmdline + cwd. Falls back to fallback_cwd and a NULL argv on other platforms
 * or when introspection fails (the caller will then save cwd only and let the
 * shell start fresh on resume). Returns true if at least cwd is populated.
 */
bool session_capture(int ptmx, const char *fallback_cwd,
                     struct session_state *out);

/* Persist to <state-dir>/<name>.json. Creates the directory if missing. */
bool session_save(const char *name, const struct session_state *st);

/* Read <state-dir>/<name>.json. */
bool session_load(const char *name, struct session_state *out);

/*
 * List existing sessions. Returns a malloc'd array of malloc'd names
 * (no .json suffix), sorted. Caller frees with session_free_names().
 */
char **session_list(size_t *out_count);
void session_free_names(char **names, size_t count);

/* Returns true if name is non-empty and contains only [A-Za-z0-9._-]. */
bool session_name_is_valid(const char *name);

/* Remove <state-dir>/<name>.json. Returns true on success. */
bool session_delete(const char *name);

/* True if <state-dir>/<name>.json exists. */
bool session_exists(const char *name);

struct terminal;

/*
 * Read <state-dir>/<name>.scrollback.txt into out_text and out_len. Returns
 * false (with out_text set to NULL) if the file is missing or fails sanity
 * checks. Caller frees out_text.
 */
bool session_load_scrollback(const char *name, char **out_text, size_t *out_len);

/* Best-effort delete of the scrollback sidecar (used when removing a session). */
void session_delete_scrollback(const char *name);

/* Encrypted-sidecar I/O. <state-dir>/<name>.scrollback.enc */
bool session_write_enc_blob(const char *name,
                            const unsigned char *blob, size_t len);
bool session_read_enc_blob(const char *name,
                           unsigned char **out, size_t *out_len);
bool session_has_enc_scrollback(const char *name);
void session_delete_enc_scrollback(const char *name);

struct terminal;

/*
 * Called from search.c when the user presses Enter while the search bar is
 * in a session prompt mode. Reads the typed name from term->search.buf and
 * performs the requested save or load, then dismisses the prompt.
 */
void session_prompt_commit(struct terminal *term);
void session_prompt_cancel(struct terminal *term);

/* Finalize a save after the user has confirmed overwrite (y). */
void session_prompt_confirm_overwrite(struct terminal *term);

/* Picker (session-load) helpers. */
void session_picker_init(struct terminal *term);
void session_picker_free(struct terminal *term);
void session_picker_refilter(struct terminal *term);
void session_picker_move(struct terminal *term, int delta);  /* +1/-1 etc */
bool session_picker_delete_selected(struct terminal *term);
