namespace FireflyClient
{
    public partial class FireflyClient
    {
        #region Hash Operations
        
        /// <summary>
        /// Sets a field in a hash
        /// </summary>
        private bool HashSet(string key, string field, string value)
        {
            var response = ExecuteCommand("HSET", key, field, value);
            return response.StartsWith(":1");
        }
        
        /// <summary>
        /// Gets a field from a hash
        /// </summary>
        private string HashGet(string key, string field)
        {
            var response = ExecuteCommand("HGET", key, field);
            return response.StartsWith('+') ? response[1..].TrimEnd('\r', '\n') : string.Empty;
        }
        
        /// <summary>
        /// Deletes a field from a hash
        /// </summary>
        private bool HashDelete(string key, string field)
        {
            var response = ExecuteCommand("HDEL", key, field);
            return response.StartsWith(":1");
        }
        
        /// <summary>
        /// Checks if a field exists in a hash
        /// </summary>
        private bool HashFieldExists(string key, string field)
        {
            var response = ExecuteCommand("HEXISTS", key, field);
            return response.StartsWith(":1");
        }
        
        /// <summary>
        /// Gets all fields and values from a hash
        /// </summary>
        private Dictionary<string, string> HashGetAll(string key)
        {
            var response = ExecuteCommand("HGETALL", key);
            var items = ParseArrayResponse(response);
            
            var result = new Dictionary<string, string>();
            for (var i = 0; i < items.Count; i += 2)
            {
                if (i + 1 < items.Count)
                {
                    result[items[i]] = items[i + 1];
                }
            }
            
            return result;
        }
        
        /// <summary>
        /// Sets multiple fields in a hash at once
        /// </summary>
        private bool HashMultiSet(string key, Dictionary<string, string> fieldValues)
        {
            List<string> args = [key];
            
            foreach (var kvp in fieldValues)
            {
                args.Add(kvp.Key);
                args.Add(kvp.Value);
            }
            
            var response = ExecuteCommand("HMSET", [.. args]);
            return response.StartsWith("+OK");
        }
        
        #endregion
    }
}