@page "/audit/execution-tree"
@attribute [Authorize(Policy = AuthorizationPolicies.OperationalAudit)]
@using ZB.MOM.WW.ScadaBridge.CentralUI.Components.Audit
@using ZB.MOM.WW.ScadaBridge.CentralUI.Services
@using ZB.MOM.WW.ScadaBridge.Commons.Types.Audit
@using ZB.MOM.WW.ScadaBridge.Security
@inject IAuditLogQueryService AuditLogQueryService

<PageTitle>Execution Chain</PageTitle>

@* Execution-chain tree view (Audit Log ParentExecutionId feature, Task 10).
   A drill-in target reached from the Audit Log drawer's "View execution chain"
   action: /audit/execution-tree?executionId={guid}. The page parses the id,
   asks the query service for the whole chain (flat ExecutionTreeNode list), and
   hands it to the recursive ExecutionTree component. There is deliberately NO
   nav-menu entry — this page is only meaningful in the context of a specific
   execution, so it is reachable only via drill-in (the Audit nav group keeps
   just the Audit Log + Configuration Audit Log pages). *@

<div class="container-fluid mt-3">
    <h1 class="h4 mb-1">Execution Chain</h1>
    <p class="text-muted small mb-3">
        The full chain of script / inbound-request executions linked by
        <span class="font-monospace">ParentExecutionId</span>, rooted at the
        topmost ancestor. Select an execution to open the Audit Log filtered to
        its rows.
    </p>

    @if (_executionId is null)
    {
        @* No (or unparseable) ?executionId= — render guidance rather than an
           empty tree. Mirrors the Audit Log page's silently-drop contract. *@
        <div class="alert alert-secondary small" data-test="execution-tree-no-id">
            No execution selected. Open this view from an audit row's
            <strong>View execution chain</strong> action.
        </div>
    }
    else if (_loading)
    {
        <div class="text-muted small" data-test="execution-tree-loading">Loading execution chain…</div>
    }
    else if (_error is not null)
    {
        <div class="alert alert-danger small" data-test="execution-tree-error">@_error</div>
    }
    else if (_nodes is { Count: > 0 })
    {
        <div class="mb-2">
            <a class="btn btn-outline-secondary btn-sm"
               data-test="execution-tree-back-to-log"
               href="@($"/audit/log?executionId={_executionId}")">
                View this execution in the Audit Log
            </a>
        </div>
        <ExecutionTree Nodes="_nodes" ArrivedFromExecutionId="_executionId.Value"
                       OnNodeActivated="HandleNodeActivated" />

        @* Double-clicking a tree node raises OnNodeActivated, which opens this
           modal for that execution. The modal renders nothing while IsOpen is
           false, so it is safe to place unconditionally here. *@
        <ExecutionDetailModal ExecutionId="_modalExecutionId" IsOpen="_modalOpen"
                              OnClose="HandleModalClose" />
    }
    else
    {
        <div class="alert alert-secondary small" data-test="execution-tree-empty">
            No execution chain found for this id.
        </div>
    }
</div>
