@use('App\Campaigns\Manager\ManagerFormat')
@php
    $nested = $children->get($entity->entity_type.'|'.$entity->external_id, collect());
    $status = ManagerFormat::status($entity->status);
    $facts = ManagerFormat::highlights($entity);
@endphp
<li class="mb-cm-node{{ $entity->removed_at ? ' is-removed' : '' }}" data-type="{{ $entity->entity_type }}" wire:key="entity-{{ $entity->id }}">
    <div class="mb-cm-node-card">
        <div class="mb-cm-node-head">
            <span class="mb-cm-node-icon" aria-hidden="true"><i class="bi {{ ManagerFormat::typeIcon($entity->entity_type) }}"></i></span>
            <div class="mb-cm-node-title">
                <div class="mb-cm-eyebrow">{{ ManagerFormat::type($entity->entity_type) }}</div>
                <div class="mb-cm-node-name">{{ $entity->name ?: '(no name)' }}</div>
                <div class="mb-cm-node-meta">
                    @if ($entity->status)
                        <span class="mb-status is-static is-{{ $status['class'] }}" title="Status set on the platform"><span class="mb-dot"></span>{{ $status['label'] }}</span>
                    @endif
                    @if ($entity->effective_status && $entity->effective_status !== $entity->status)
                        @php $effective = ManagerFormat::status($entity->effective_status); @endphp
                        <span class="mb-status is-static is-{{ $effective['class'] }}" title="What the platform says is actually happening"><span class="mb-dot"></span>{{ $effective['label'] }}</span>
                    @endif
                    @if ($entity->removed_at)
                        <span class="mb-cm-flag is-gone"><i class="bi bi-x-circle"></i> Not on platform since {{ $entity->removed_at->diffForHumans() }}</span>
                    @elseif ($entity->last_changed_at)
                        <span class="mb-cm-flag is-changed" title="{{ $entity->last_changed_at->toDayDateTimeString() }}"><i class="bi bi-activity"></i> Changed {{ $entity->last_changed_at->diffForHumans() }}</span>
                    @endif
                    <span class="mb-cell-meta mb-cm-mono" title="Platform id">{{ $entity->external_id }}</span>
                </div>
            </div>
            @if ($capabilities->editableFields($entity) !== [])
                <div class="mb-cm-node-actions">
                    <a href="{{ route('campaign-manager.edit', [$entity->campaign_id, $entity]) }}" class="mb-btn-ghost is-sm" wire:navigate aria-label="Edit {{ $entity->name ?: $entity->external_id }}">
                        <i class="bi bi-pencil"></i> Edit
                    </a>
                </div>
            @endif
        </div>

        @if ($facts !== [])
            <div class="mb-cm-facts">
                @foreach ($facts as $fact)
                    <div class="mb-cm-fact">
                        <span class="mb-cm-fact-label"><i class="bi {{ $fact['icon'] }}"></i> {{ $fact['label'] }}</span>
                        <span class="mb-cm-fact-value">{{ $fact['value'] }}</span>
                    </div>
                @endforeach
            </div>
        @endif

        @if (! empty($entity->settings))
            <details class="mb-cm-more">
                <summary><i class="bi bi-chevron-right"></i> All settings · seen on the platform {{ $entity->last_seen_at->diffForHumans() }}</summary>
                <dl class="mb-cm-kv">
                    @foreach (collect($entity->settings)->sortKeys() as $field => $value)
                        <dt>{{ ManagerFormat::field($field) }}</dt>
                        <dd>
                            @if (ManagerFormat::isLong($value))
                                <details><summary>Show</summary><pre>{{ ManagerFormat::value($value) }}</pre></details>
                            @else
                                {{ ManagerFormat::setting($field, $value, $entity) }}
                            @endif
                        </dd>
                    @endforeach
                </dl>
            </details>
        @endif
    </div>

    @if ($nested->isNotEmpty() && $depth < 4)
        <ul class="mb-cm-tree">
            @foreach ($nested as $child)
                @include('livewire.campaign-manager.partials.entity', ['entity' => $child, 'depth' => $depth + 1])
            @endforeach
        </ul>
    @endif
</li>
