#pragma warning disable IDE0130 // Namespace does not match folder structure
namespace Firefly.Protocol
#pragma warning restore IDE0130 // Namespace does not match folder structure
{
    /// <summary>
    /// Defines the Firefly wire protocol specification for 3rd party clients
    /// </summary>
    public static class FireflyProtocol
    {
        /// <summary>
        /// The current version of the Firefly protocol
        /// </summary>
        public const string PROTOCOL_VERSION = "1.0";

        // Command prefixes
        /// <summary>
        /// Prefix for simple string responses (+)
        /// </summary>
        public const char STRING_MESSAGE = '+';

        /// <summary>
        /// Prefix for error responses (-)
        /// </summary>
        public const char ERROR_MESSAGE = '-';

        /// <summary>
        /// Prefix for integer responses (:)
        /// </summary>
        public const char INTEGER_MESSAGE = ':';

        /// <summary>
        /// Prefix for bulk string responses ($)
        /// </summary>
        public const char BULK_MESSAGE = '$';

        /// <summary>
        /// Prefix for array responses (*)
        /// </summary>
        public const char ARRAY_MESSAGE = '*';

        /// <summary>
        /// Special message indicating a nil value ($-1)
        /// </summary>
        public const string NIL_MESSAGE = "$-1";

        /// <summary>
        /// Standard message terminator sequence (\r\n)
        /// </summary>
        public const string MESSAGE_TERMINATOR = "\r\n";

        /// <summary>
        /// Standard OK response (+OK\r\n)
        /// </summary>
        public const string OK_RESPONSE = "+OK\r\n";

        /// <summary>
        /// Response for queued pipeline commands (+QUEUED\r\n)
        /// </summary>
        public const string QUEUED_RESPONSE = "+QUEUED\r\n";

        /// <summary>
        /// Standard nil response ($-1\r\n)
        /// </summary>
        public const string NIL_RESPONSE = "$-1\r\n";

        /// <summary>
        /// Command format specification for external clients
        /// </summary>
        public static class Commands
        {
            // Authentication
            /// <summary>
            /// Authenticate with the server using a password
            /// </summary>
            public const string AUTH = "AUTH {password}";

            /// <summary>
            /// Test server connection
            /// </summary>
            public const string PING = "PING";

            /// <summary>
            /// Close the connection
            /// </summary>
            public const string QUIT = "QUIT";

            // String operations
            /// <summary>
            /// Set a key to hold a string value
            /// </summary>
            public const string SET = "SET {key} {value}";

            /// <summary>
            /// Get the value of a key
            /// </summary>
            public const string GET = "GET {key}";

            /// <summary>
            /// Delete a key
            /// </summary>
            public const string DEL = "DEL {key}";

            // List operations
            /// <summary>
            /// Insert elements at the head of a list
            /// </summary>
            public const string LPUSH = "LPUSH {key} {value} [value...]";

            /// <summary>
            /// Insert elements at the tail of a list
            /// </summary>
            public const string RPUSH = "RPUSH {key} {value} [value...]";

            /// <summary>
            /// Remove and get the first element in a list
            /// </summary>
            public const string LPOP = "LPOP {key}";

            /// <summary>
            /// Remove and get the last element in a list
            /// </summary>
            public const string RPOP = "RPOP {key}";

            /// <summary>
            /// Get a range of elements from a list
            /// </summary>
            public const string LRANGE = "LRANGE {key} {start} {stop}";

            // Hash operations
            /// <summary>
            /// Set the string value of a hash field
            /// </summary>
            public const string HSET = "HSET {key} {field} {value}";

            /// <summary>
            /// Get the value of a hash field
            /// </summary>
            public const string HGET = "HGET {key} {field}";

            /// <summary>
            /// Delete a hash field
            /// </summary>
            public const string HDEL = "HDEL {key} {field}";

            /// <summary>
            /// Get all fields and values in a hash
            /// </summary>
            public const string HGETALL = "HGETALL {key}";

            // Pipeline operations
            /// <summary>
            /// Start a pipeline for batch processing
            /// </summary>
            public const string PIPELINE = "PIPELINE";

            /// <summary>
            /// Execute all commands in the pipeline
            /// </summary>
            public const string EXEC = "EXEC";
        }

        /// <summary>
        /// Response format specification for external clients
        /// </summary>
        public static class Responses
        {
            /// <summary>
            /// Format a simple string response
            /// </summary>
            /// <param name="value">The string value to format</param>
            /// <returns>Formatted string response</returns>
            public static string FormatString(string value) => $"+{value}{MESSAGE_TERMINATOR}";

            /// <summary>
            /// Format an error response
            /// </summary>
            /// <param name="message">The error message</param>
            /// <returns>Formatted error response</returns>
            public static string FormatError(string message) => $"-ERR {message}{MESSAGE_TERMINATOR}";

            /// <summary>
            /// Format an integer response
            /// </summary>
            /// <param name="value">The integer value to format</param>
            /// <returns>Formatted integer response</returns>
            public static string FormatInteger(int value) => $":{value}{MESSAGE_TERMINATOR}";

            /// <summary>
            /// Format a bulk string response
            /// </summary>
            /// <param name="value">The string value to format</param>
            /// <returns>Formatted bulk string response</returns>
            public static string FormatBulkString(string value) => $"${value.Length}{MESSAGE_TERMINATOR}{value}{MESSAGE_TERMINATOR}";

            /// <summary>
            /// Format an array response
            /// </summary>
            /// <param name="values">Array of strings to format</param>
            /// <returns>Formatted array response</returns>
            public static string FormatArray(string[] values)
            {
                var result = $"*{values.Length}{MESSAGE_TERMINATOR}";
                foreach (var value in values)
                {
                    result += FormatBulkString(value);
                }
                return result;
            }
        }
    }
}