#ifndef TRANSTUI_CONFIG_H
#define TRANSTUI_CONFIG_H

#include <stddef.h>

typedef struct {
    char host[256];
    int port;
    char username[128];
    char password[128];
    int poll_interval_ms;
    int show_splash;
    char path[1024]; /* resolved config file path, for messages */

    /* Cache of the session settings (download dir, speed limits, ...) the
     * user last explicitly set via the in-app settings screen.
     * transmission-daemon only writes its own settings.json to disk on a
     * clean shutdown, not immediately when changed over RPC - if it's later
     * killed uncleanly (crash, power loss, kill -9) before that, the change
     * is lost and the daemon reverts to whatever it last saved. transtui
     * re-applies this cache whenever the daemon's current session doesn't
     * match it (see ui_settings.c), so an intentional change survives an
     * unclean daemon restart even though the daemon itself forgot it. */
    int session_cache_set; /* 0 until a session field has been changed at least once */
    char cache_download_dir[512];
    long cache_speed_down;
    int cache_speed_down_enabled;
    long cache_speed_up;
    int cache_speed_up_enabled;
    int cache_alt_enabled;
    long cache_alt_down;
    long cache_alt_up;
    int cache_alt_time_enabled;
    int cache_alt_time_begin;
    int cache_alt_time_end;
    double cache_seed_ratio_limit;
    int cache_seed_ratio_enabled;
} Config;

/* Loads ~/.config/transtui/config.ini (or $XDG_CONFIG_HOME/transtui/config.ini).
 * Creates the file with sane defaults if it doesn't exist yet.
 * Returns 0 on success (cfg always ends up with usable defaults), -1 only if
 * the config directory couldn't be created/read at all (err filled). */
int config_load(Config *cfg, char *err, size_t errlen);

/* Writes cfg back to cfg->path (as resolved by config_load). Used when the
 * user edits daemon/download settings from the in-app settings screen. */
int config_save(const Config *cfg, char *err, size_t errlen);

/* Applies -H/-P/-u/-p/-i command-line overrides on top of an already-loaded
 * config. Prints usage and exits on -h/--help or a bad argument. If -A/--add
 * <source> is given, its value is copied into add_source (left empty
 * otherwise) - the caller uses that to switch into non-interactive
 * add-and-exit mode instead of launching the TUI. */
void config_apply_args(Config *cfg, int argc, char **argv, char *add_source, size_t add_source_size);

#endif
