using ZB.MOM.WW.Audit;
using ZB.MOM.WW.ScadaBridge.Commons.Types.Audit;
using ZB.MOM.WW.ScadaBridge.Commons.Types.Enums;

namespace ZB.MOM.WW.ScadaBridge.CentralUI.Services;

/// <summary>
/// Flattened, typed view of a canonical <see cref="ZB.MOM.WW.Audit.AuditEvent"/> for the
/// Central UI audit pages. The canonical record is the seam type — the
/// query service decomposes it into this view (via <see cref="AuditRowProjection"/>) so the
/// existing razor bindings (<c>row.Channel</c>, <c>Event.Status</c>, <c>evt.RequestSummary</c>,
/// …) keep working against typed properties rather than parsing <c>DetailsJson</c> inline.
/// </summary>
/// <remarks>
/// This is presentation-only: it carries the same field surface the bespoke
/// <c>Commons.Entities.Audit.AuditEvent</c> exposed previously. <c>ForwardState</c> is always
/// null on the central read path (it is site-storage-only and not carried on canonical rows).
/// </remarks>
public sealed record AuditEventView
{
    /// <summary>Idempotency key.</summary>
    public Guid EventId { get; init; }
    /// <summary>UTC timestamp when the audited action occurred.</summary>
    public DateTime OccurredAtUtc { get; init; }
    /// <summary>UTC ingest timestamp (central-set); null until ingest.</summary>
    public DateTime? IngestedAtUtc { get; init; }
    /// <summary>Trust-boundary channel.</summary>
    public AuditChannel Channel { get; init; }
    /// <summary>Specific event kind.</summary>
    public AuditKind Kind { get; init; }
    /// <summary>Per-operation correlation id.</summary>
    public Guid? CorrelationId { get; init; }
    /// <summary>Originating execution id.</summary>
    public Guid? ExecutionId { get; init; }
    /// <summary>Spawning execution id; null for top-level runs.</summary>
    public Guid? ParentExecutionId { get; init; }
    /// <summary>Site id where the action originated.</summary>
    public string? SourceSiteId { get; init; }
    /// <summary>Cluster node that emitted the event.</summary>
    public string? SourceNode { get; init; }
    /// <summary>Instance id where the action originated.</summary>
    public string? SourceInstanceId { get; init; }
    /// <summary>Script that initiated the action.</summary>
    public string? SourceScript { get; init; }
    /// <summary>Authenticated actor.</summary>
    public string? Actor { get; init; }
    /// <summary>Target of the action.</summary>
    public string? Target { get; init; }
    /// <summary>Lifecycle status.</summary>
    public AuditStatus Status { get; init; }
    /// <summary>HTTP status code where applicable.</summary>
    public int? HttpStatus { get; init; }
    /// <summary>Duration of the action in ms.</summary>
    public int? DurationMs { get; init; }
    /// <summary>Human-readable error summary.</summary>
    public string? ErrorMessage { get; init; }
    /// <summary>Verbose error detail.</summary>
    public string? ErrorDetail { get; init; }
    /// <summary>Truncated/redacted request summary.</summary>
    public string? RequestSummary { get; init; }
    /// <summary>Truncated/redacted response summary.</summary>
    public string? ResponseSummary { get; init; }
    /// <summary>True when summaries were truncated.</summary>
    public bool PayloadTruncated { get; init; }
    /// <summary>Free-form JSON extension.</summary>
    public string? Extra { get; init; }
    /// <summary>Site-local forwarding state; always null on the central read path.</summary>
    public AuditForwardState? ForwardState { get; init; }

    /// <summary>
    /// Decomposes a canonical <see cref="AuditEvent"/> into a flat view for the UI.
    /// </summary>
    /// <param name="evt">The canonical audit event to decompose.</param>
    /// <returns>A flat <see cref="AuditEventView"/> populated from the event's top-level and details fields.</returns>
    public static AuditEventView From(AuditEvent evt)
    {
        var r = AuditRowProjection.Decompose(evt);
        return new AuditEventView
        {
            EventId = r.EventId,
            OccurredAtUtc = r.OccurredAtUtc,
            IngestedAtUtc = r.IngestedAtUtc,
            Channel = r.Channel,
            Kind = r.Kind,
            CorrelationId = r.CorrelationId,
            ExecutionId = r.ExecutionId,
            ParentExecutionId = r.ParentExecutionId,
            SourceSiteId = r.SourceSiteId,
            SourceNode = r.SourceNode,
            SourceInstanceId = r.SourceInstanceId,
            SourceScript = r.SourceScript,
            Actor = r.Actor,
            Target = r.Target,
            Status = r.Status,
            HttpStatus = r.HttpStatus,
            DurationMs = r.DurationMs,
            ErrorMessage = r.ErrorMessage,
            ErrorDetail = r.ErrorDetail,
            RequestSummary = r.RequestSummary,
            ResponseSummary = r.ResponseSummary,
            PayloadTruncated = r.PayloadTruncated,
            Extra = r.Extra,
            ForwardState = null,
        };
    }
}
