using IdokladSdk.Enums;
namespace Idoklad.Configuration;
///
/// Service configuration resolved from environment variables.
///
/// Mirrors the structure used by the sibling microsoft-365-service (config.py): non-secret
/// configuration and credential defaults live in environment variables. Per-request callers
/// can override the credential values through request headers (see
/// ).
///
public sealed class IdokladSettings
{
public string AppName { get; init; } = GetEnv("APP_NAME", "iDoklad Service");
public string AppVersion { get; init; } = GetEnv("APP_VERSION", "1.0.0");
public string RootPath { get; init; } = GetEnv("ROOT_PATH", string.Empty);
/// Default iDoklad OAuth2 client id (client credentials flow).
public string ClientId { get; init; } = GetEnv("IDOKLAD_CLIENT_ID", string.Empty);
/// Default iDoklad OAuth2 client secret (client credentials flow).
public string ClientSecret { get; init; } = GetEnv("IDOKLAD_CLIENT_SECRET", string.Empty);
/// Default iDoklad application id from the developer portal (required by client credentials flow).
public string ApplicationId { get; init; } = GetEnv("IDOKLAD_APPLICATION_ID", string.Empty);
/// Optional custom iDoklad API base url (defaults to the SDK production url when empty).
public string ApiUrl { get; init; } = GetEnv("IDOKLAD_API_URL", string.Empty);
/// Optional custom Identity Server token url (defaults to the SDK production url when empty).
public string IdentityServerUrl { get; init; } = GetEnv("IDOKLAD_IDENTITY_URL", string.Empty);
/// Default response language for the iDoklad API (Cz, Sk, En). Defaults to Cz.
public Language Language { get; init; } = ParseLanguage(GetEnv("IDOKLAD_LANGUAGE", "Cz"));
public int RequestTimeoutSeconds { get; init; } = ParseInt(GetEnv("IDOKLAD_REQUEST_TIMEOUT_SECONDS", "100"), 100);
/// True when both custom urls are configured (e.g. for a sandbox environment).
public bool HasCustomUrls => !string.IsNullOrWhiteSpace(ApiUrl) && !string.IsNullOrWhiteSpace(IdentityServerUrl);
/// True when the default credential triplet is fully configured via environment variables.
public bool HasDefaultCredentials =>
!string.IsNullOrWhiteSpace(ClientId)
&& !string.IsNullOrWhiteSpace(ClientSecret)
&& !string.IsNullOrWhiteSpace(ApplicationId);
private static string GetEnv(string name, string fallback)
{
var value = Environment.GetEnvironmentVariable(name);
return string.IsNullOrWhiteSpace(value) ? fallback : value;
}
private static int ParseInt(string value, int fallback) =>
int.TryParse(value, out var parsed) && parsed > 0 ? parsed : fallback;
private static Language ParseLanguage(string value) =>
Enum.TryParse(value, ignoreCase: true, out var parsed) ? parsed : Language.Cz;
}