namespace ZB.MOM.WW.ScadaBridge.Commons.Messages.Management;
// ─────────────────────────────────────────────────────────────────────────────
// OPC UA server-certificate trust management.
//
// Cert trust is SITE-LOCAL: there is no central persistence of trusted server
// certificates. The trusted-peer PKI store lives on each site node's file
// system, so a trust/remove decision must reach BOTH site nodes (node-a and
// node-b) or the two PKI stores diverge across failover.
//
// The public commands below are handled by the site Deployment Manager
// singleton (active node only). For trust/remove it broadcasts the
// corresponding per-node internal message to the per-node CertStoreActor on
// EVERY site cluster node; for list it answers from the local node.
//
// The decision itself IS recorded centrally (Gitea #43): both central entry points (the
// management actor and the Central UI CertManagementService) write an AuditChannel.Security
// AuditLog row (AuditKind.CertTrust / CertTrustRemove) naming the actor, site and thumbprint,
// with Delivered/Failed per the aggregate site outcome. Only the certificate itself stays site-local.
// ─────────────────────────────────────────────────────────────────────────────
///
/// Trust an OPC UA server certificate at every site node. Carries the raw
/// DER bytes (base64-encoded) so the certificate can be written into each
/// node's trusted-peer PKI store. The thumbprint is the store filename key.
///
/// The data-connection the certificate was captured from (diagnostics / correlation only).
/// The server certificate's DER encoding, base64-encoded.
/// The certificate thumbprint — used as the store filename key.
///
/// Target site identifier — REQUIRED when the command is invoked via the management
/// HTTP API/CLI (the actor must route it to a site); ignored on the site side, where
/// routing already happened. Additive per the message-contract evolution rules.
///
public record TrustServerCertCommand(string ConnectionName, string DerBase64, string Thumbprint, string? SiteIdentifier = null);
///
/// Remove a previously-trusted OPC UA server certificate from every site
/// node's trusted-peer PKI store, identified by thumbprint.
///
/// The thumbprint of the certificate to remove.
///
/// Target site identifier — REQUIRED when the command is invoked via the management
/// HTTP API/CLI (the actor must route it to a site); ignored on the site side, where
/// routing already happened. Additive per the message-contract evolution rules.
///
public record RemoveServerCertCommand(string Thumbprint, string? SiteIdentifier = null);
///
/// List the certificates currently present in this site's trusted-peer and
/// rejected PKI stores. Answered from the local node (the Deployment Manager
/// singleton's own node).
///
///
/// Target site identifier — REQUIRED when the command is invoked via the management
/// HTTP API/CLI (the actor must route it to a site); ignored on the site side, where
/// routing already happened. Additive per the message-contract evolution rules.
///
public record ListServerCertsCommand(string? SiteIdentifier = null);
///
/// Read-only projection of a certificate found in a site PKI store.
///
/// The certificate thumbprint.
/// The certificate subject distinguished name.
/// The certificate issuer distinguished name.
/// Validity start (UTC).
/// Validity end (UTC).
/// True if the certificate is in the rejected store; false if trusted.
public record TrustedCertInfo(
string Thumbprint,
string Subject,
string Issuer,
DateTime NotBeforeUtc,
DateTime NotAfterUtc,
bool Rejected);
///
/// Aggregate result of a cert-trust command. For trust/remove,
/// reflects whether every reachable site node acked; carries the
/// first node error (or a partial-failure note when a node did not ack in time).
/// For list, carries the local node's store contents.
///
/// True when the operation succeeded on all targeted nodes.
/// First error encountered, or null on success.
/// Listed certificates (list command only), otherwise null.
public record CertTrustResult(bool Success, string? Error, IReadOnlyList? Certs);
// ── Per-node internal messages (CertStoreActor wire protocol) ──
// Sent by the Deployment Manager singleton to each site node's CertStoreActor,
// or used directly in tests. Not part of the public management surface.
/// Per-node: decode and write it into the local trusted-peer store as <Thumbprint>.der.
/// The certificate's DER encoding, base64-encoded.
/// The thumbprint used as the store filename key.
public record WriteCertToLocalStore(string DerBase64, string Thumbprint);
/// Per-node: delete the trusted-peer store file matching .
/// The thumbprint of the certificate to remove.
public record RemoveCertFromLocalStore(string Thumbprint);
/// Per-node: enumerate the local trusted-peer and rejected stores.
public record ListLocalCerts();
///
/// Per-node: export the trusted-peer store contents (thumbprint + raw DER bytes)
/// so the Deployment Manager singleton can reconcile the trusted set onto a
/// (re)joining site node. Trusted store only — rejected certs are never pushed.
///
public record ExportLocalTrustedCerts();
///
/// One exported trusted certificate: its thumbprint (store filename key) and the
/// raw DER bytes needed to re-write it into another node's trusted-peer store.
///
/// The certificate thumbprint.
/// The certificate's raw DER encoding.
public record ExportedCert(string Thumbprint, byte[] DerBytes);
///
/// Per-node reply to : the local trusted-peer
/// store's certificates, or a failure with the IO error.
///
/// True if the trusted store was enumerated successfully.
/// The error message on failure, otherwise null.
/// Exported trusted certificates, empty on failure.
public record LocalCertExport(bool Success, string? Error, IReadOnlyList Certs);
///
/// Per-node ack for a ,
/// or operation.
///
/// True if the local store operation succeeded.
/// The error message on failure, otherwise null.
/// Listed certificates (list only), otherwise null.
public record LocalCertOpAck(bool Success, string? Error, IReadOnlyList? Certs);