namespace StorageService.Infrastructure.Configuration;

/// <summary>
/// EN: Redis cache settings.
/// VI: Cấu hình Redis cache.
/// </summary>
public class RedisSettings
{
    public const string SectionName = "Redis";
    
    /// <summary>EN: Redis host / VI: Redis host</summary>
    public string Host { get; set; } = "localhost";
    
    /// <summary>EN: Redis port / VI: Redis port</summary>
    public int Port { get; set; } = 6379;
    
    /// <summary>EN: Redis password (optional) / VI: Redis password (tùy chọn)</summary>
    public string? Password { get; set; }
    
    /// <summary>EN: Redis database index / VI: Redis database index</summary>
    public int Database { get; set; } = 0;
    
    /// <summary>EN: Connection timeout in ms / VI: Timeout kết nối (ms)</summary>
    public int ConnectTimeout { get; set; } = 5000;
    
    /// <summary>EN: Sync operation timeout in ms / VI: Timeout thao tác đồng bộ (ms)</summary>
    public int SyncTimeout { get; set; } = 5000;
    
    /// <summary>EN: Enable Redis caching / VI: Bật Redis caching</summary>
    public bool Enabled { get; set; } = true;
    
    /// <summary>
    /// EN: Build connection string for StackExchange.Redis.
    /// VI: Xây dựng connection string cho StackExchange.Redis.
    /// </summary>
    public string GetConnectionString()
    {
        var connectionString = $"{Host}:{Port},connectTimeout={ConnectTimeout},syncTimeout={SyncTimeout}";
        
        if (!string.IsNullOrWhiteSpace(Password))
        {
            connectionString += $",password={Password}";
        }
        
        if (Database > 0)
        {
            connectionString += $",defaultDatabase={Database}";
        }
        
        connectionString += ",abortConnect=false";
        
        return connectionString;
    }
}
