using Microsoft.AspNetCore.Components;
using ZB.MOM.WW.ScadaBridge.CentralUI.Services;

namespace ZB.MOM.WW.ScadaBridge.CentralUI.Components.Audit;

/// <summary>
/// Child component for the central Audit Log page.
/// Renders one <see cref="AuditEventView"/> in a right-side off-canvas drawer.
/// The drawer owns only the offcanvas chrome — backdrop, header, and the two
/// Close buttons; the single-row detail body (read-only fields, conditional
/// Error/Request/Response/Extra subsections, and action buttons) is delegated
/// to <see cref="AuditEventDetail"/>, which is shared with the execution-tree
/// node-detail modal so a row's detail renders identically in either host.
/// The drawer is fully presentational — it has no DB or service dependencies;
/// the host page owns the open/close state.
/// </summary>
public partial class AuditDrilldownDrawer
{
    /// <summary>
    /// The row to render. When null the drawer renders nothing — the host
    /// page uses this together with <see cref="IsOpen"/> to drive visibility.
    /// </summary>
    [Parameter] public AuditEventView? Event { get; set; }

    /// <summary>
    /// True when the host wants the drawer visible. We deliberately keep
    /// this as a separate parameter from <see cref="Event"/>: an open
    /// drawer briefly with a null event renders nothing (the row may still
    /// be loading); a closed drawer with a stale event is the resting state.
    /// </summary>
    [Parameter] public bool IsOpen { get; set; }

    /// <summary>
    /// Fired when the user dismisses the drawer (close button or backdrop
    /// click). The host is expected to flip <see cref="IsOpen"/> to false.
    /// </summary>
    [Parameter] public EventCallback OnClose { get; set; }

    private static string ShortEventId(Guid eventId)
    {
        // Mirror the "first 8 hex digits" presentation common across the UI.
        var n = eventId.ToString("N");
        return n.Length >= 8 ? n[..8] : n;
    }

    private async Task HandleClose()
    {
        if (OnClose.HasDelegate)
        {
            await OnClose.InvokeAsync();
        }
    }
}
