using Common.Enums;
using Microsoft.Extensions.Configuration;
using Newtonsoft.Json;
namespace Common.Configuration.DownloadClient;
///
/// Configuration for a specific download client
///
public sealed record ClientConfig
{
///
/// Whether this client is enabled
///
public bool Enabled { get; init; } = true;
///
/// Unique identifier for this client
///
public Guid Id { get; init; } = Guid.NewGuid();
///
/// Friendly name for this client
///
public string Name { get; init; } = string.Empty;
///
/// Type of download client
///
public DownloadClientType Type { get; init; } = DownloadClientType.None;
///
/// Host address for the download client
///
public string Host { get; init; } = string.Empty;
///
/// Username for authentication
///
public string Username { get; init; } = string.Empty;
///
/// Password for authentication
///
public string Password { get; init; } = string.Empty;
///
/// The base URL path component, used by clients like Transmission and Deluge
///
[JsonProperty("url_base")]
public string UrlBase { get; init; } = string.Empty;
///
/// The computed full URL for the client
///
public Uri Url => new($"{Host.TrimEnd('/')}/{UrlBase.TrimStart('/').TrimEnd('/')}");
///
/// Validates the configuration
///
public void Validate()
{
if (Id == Guid.Empty)
{
throw new InvalidOperationException("Client ID cannot be empty");
}
if (string.IsNullOrWhiteSpace(Name))
{
throw new InvalidOperationException($"Client name cannot be empty for client ID: {Id}");
}
if (string.IsNullOrWhiteSpace(Host))
{
throw new InvalidOperationException($"Host cannot be empty for client ID: {Id}");
}
if (Type == DownloadClientType.None)
{
throw new InvalidOperationException($"Client type must be specified for client ID: {Id}");
}
}
}