using Akka.Configuration;

namespace ZB.MOM.WW.Secrets.Replicator.AkkaDotNet;

/// <summary>
/// Akka configuration this package contributes.
/// </summary>
public static class AkkaSecretsReplication
{
    /// <summary>
    /// HOCON binding the secret-replication wire protocol to
    /// <see cref="SecretReplicationSerializer"/>. Merge it into the application's Akka
    /// configuration when creating the <c>ActorSystem</c>:
    /// <code>
    /// Config config = AkkaSecretsReplication.SerializationConfig
    ///     .WithFallback(myAppConfig);
    /// ActorSystem.Create("my-system", config);
    /// </code>
    /// </summary>
    /// <remarks>
    /// Optional — the protocol DTOs round-trip under Akka's default JSON serializer without it — but
    /// recommended, because it makes the serializer carrying secret ciphertext an explicit choice
    /// rather than an inherited default, and pins the manifest strings that a future protocol
    /// version would bump.
    /// </remarks>
    public static Config SerializationConfig { get; } = ConfigurationFactory.ParseString($$"""
        akka.actor {
          serializers {
            zb-secrets-replication = "{{typeof(SecretReplicationSerializer).AssemblyQualifiedName}}"
          }
          serialization-bindings {
            "ZB.MOM.WW.Secrets.Replicator.AkkaDotNet.Protocol.SecretRowsMessage, ZB.MOM.WW.Secrets.Replicator.AkkaDotNet" = zb-secrets-replication
            "ZB.MOM.WW.Secrets.Replicator.AkkaDotNet.Protocol.SecretManifestAnnounce, ZB.MOM.WW.Secrets.Replicator.AkkaDotNet" = zb-secrets-replication
            "ZB.MOM.WW.Secrets.Replicator.AkkaDotNet.Protocol.SecretPullRequest, ZB.MOM.WW.Secrets.Replicator.AkkaDotNet" = zb-secrets-replication
          }
          serialization-identifiers {
            "{{typeof(SecretReplicationSerializer).AssemblyQualifiedName}}" = {{SecretReplicationSerializer.SerializerId}}
          }
        }
        """);
}
