<div class="row g-3 mt-1">
    <div class="col-12">
        <x-crm-file-manager module="tickets" :record="$ticket" />
    </div>
    @if ($ticket->moved_at)
        <div class="col-12">
            <div class="alert alert-info">
                This Ticket was moved to <strong>{{ ucfirst($ticket->moved_to_type) }} #{{ $ticket->moved_to_id }}</strong>
                on {{ $ticket->moved_at->format('M d, Y H:i') }}. Its original profile and relationships remain available here.
            </div>
        </div>
    @endif

    <div class="col-lg-6">
        <div class="card stretch stretch-full">
            <div class="card-header"><h5 class="card-title">Ticket relationships</h5></div>
            <div class="card-body">
                <dl class="row mb-0">
                    <dt class="col-4">Campaign</dt><dd class="col-8">{{ $ticket->campaign?->campaign_name ?? 'Not linked' }}</dd>
                    <dt class="col-4">Account</dt><dd class="col-8">{{ $ticket->account?->name ?? $ticket->account?->account_name ?? 'Not linked' }}</dd>
                    <dt class="col-4">Contact</dt><dd class="col-8">{{ trim(($ticket->primaryContact?->first_name ?? '').' '.($ticket->primaryContact?->last_name ?? '')) ?: 'Not linked' }}</dd>
                    <dt class="col-4">Products</dt>
                    <dd class="col-8">{{ $ticket->ticketProducts->map(fn ($line) => $line->product?->name)->filter()->join(', ') ?: 'None' }}</dd>
                    <dt class="col-4">Description</dt><dd class="col-8">{{ $ticket->description ?: '—' }}</dd>
                    <dt class="col-4">Comments</dt><dd class="col-8">{{ $ticket->comments ?: '—' }}</dd>
                </dl>
            </div>
        </div>
    </div>

    <div class="col-lg-6">
        <div class="card stretch stretch-full">
            <div class="card-header"><h5 class="card-title">Assignment history</h5></div>
            <div class="card-body">
                @forelse ($ticket->assignmentHistories as $history)
                    <div class="border-bottom pb-2 mb-2">
                        <strong>{{ $history->fromUser?->name ?? 'Unassigned' }}</strong>
                        <i class="feather-arrow-right mx-1"></i>
                        <strong>{{ $history->toUser?->name ?? 'Unassigned' }}</strong>
                        <div class="small text-muted">{{ $history->created_at->format('M d, Y H:i') }} by {{ $history->actor?->name ?? 'System' }}</div>
                        @if ($history->note)<div>{{ $history->note }}</div>@endif
                    </div>
                @empty
                    <p class="text-muted mb-0">No reassignment has been recorded.</p>
                @endforelse
            </div>
        </div>
    </div>

    <div class="col-lg-6">
        <div class="card stretch stretch-full">
            <div class="card-header"><h5 class="card-title">Status timeline</h5></div>
            <div class="card-body">
                @forelse ($ticket->statusHistory->sortByDesc('changed_at') as $history)
                    <div class="border-bottom pb-2 mb-2">
                        {{ $history->from_status ?: 'Created' }} <i class="feather-arrow-right mx-1"></i> <strong>{{ $history->to_status }}</strong>
                        <div class="small text-muted">{{ $history->changed_at->format('M d, Y H:i') }} by {{ $history->changedBy?->name ?? 'System' }}</div>
                        @if ($history->reason)<div>{{ $history->reason }}</div>@endif
                    </div>
                @empty
                    <p class="text-muted mb-0">No status changes have been recorded.</p>
                @endforelse
            </div>
        </div>
    </div>

    <div class="col-lg-6">
        <div class="card stretch stretch-full">
            <div class="card-header"><h5 class="card-title">Move history</h5></div>
            <div class="card-body">
                @forelse ($ticket->moveHistories->sortByDesc('created_at') as $history)
                    <div class="border-bottom pb-2 mb-2">
                        Moved to <strong>{{ ucfirst($history->destination_type) }} #{{ $history->destination_id }}</strong>
                        <div class="small text-muted">{{ $history->created_at->format('M d, Y H:i') }} by {{ $history->actor?->name ?? 'System' }}</div>
                        <div class="small text-muted">
                            Preserved: {{ count($history->relationship_snapshot['activity_ids'] ?? []) }} activities,
                            {{ count($history->relationship_snapshot['product_ids'] ?? []) }} products,
                            {{ count($history->relationship_snapshot['file_ids'] ?? []) }} files.
                        </div>
                    </div>
                @empty
                    <p class="text-muted mb-0">This Ticket has not been moved.</p>
                @endforelse
            </div>
        </div>
    </div>

    <div class="col-12">
        <div class="card">
            <div class="card-header"><h5 class="card-title">Activities and files</h5></div>
            <div class="card-body">
                <p><strong>Activities:</strong> {{ $ticket->activities->count() }} &nbsp; <strong>Files:</strong> {{ $ticket->files->count() }}</p>
                @forelse ($ticket->activities->sortByDesc('created_at') as $activity)
                    <div class="border-bottom py-2"><strong>{{ $activity->title ?? $activity->subject ?? 'Activity' }}</strong>
                        <span class="text-muted ms-2">{{ $activity->created_at?->format('M d, Y H:i') }}</span></div>
                @empty
                    <p class="text-muted mb-0">No Ticket activities found.</p>
                @endforelse
            </div>
        </div>
    </div>
    <div class="col-12">
        <x-profile-notification-history entity-type="Ticket" :entity-id="$ticket->id" />
    </div>
</div>
