using ZB.MOM.WW.Configuration;

namespace ZB.MOM.WW.ScadaBridge.Host;

/// <summary>
/// Validates <see cref="DatabaseOptions"/> at host startup (arch-review 08
/// round 2 NF4). All three connection settings are nullable and role-dependent
/// (central needs the SQL Server strings, a site needs only the SQLite path),
/// so a <c>null</c> value is valid. A present-but-whitespace value is not — it
/// signals a mis-set config key that would fail opaquely at first DB use.
/// </summary>
public sealed class DatabaseOptionsValidator : OptionsValidatorBase<DatabaseOptions>
{
    /// <inheritdoc />
    protected override void Validate(ValidationBuilder builder, DatabaseOptions options)
    {
        RequireNotWhitespace(builder, options.ConfigurationDb, nameof(DatabaseOptions.ConfigurationDb));
        RequireNotWhitespace(builder, options.MachineDataDb, nameof(DatabaseOptions.MachineDataDb));
        RequireNotWhitespace(builder, options.SiteDbPath, nameof(DatabaseOptions.SiteDbPath));
    }

    // null is valid (role-dependent); reject only a present-but-blank value.
    private static void RequireNotWhitespace(ValidationBuilder builder, string? value, string field)
    {
        builder.RequireThat(value is null || !string.IsNullOrWhiteSpace(value),
            $"ScadaBridge:Database:{field} is set but blank — either remove it or give it a real value.");
    }
}
