@php
    $isHighPriority =
        ($client->date_of_birth?->age >= 68)
        || ($client->claim?->terminalIllness !== null);
@endphp

<div class="card">
    <div class="card-body">

        {{-- HEADER --}}
        <div class="d-flex justify-content-between align-items-center mb-3">
            <div class="d-flex align-items-center gap-2">
                <h4 class="fw-bold mb-0">
                    Client Details
                </h4>
            </div>

            {{-- ACTION DROPDOWN --}}
            @can('edit', $client)
                <a wire:navigate href="{{ route('clients.edit', $client) }}"
                   class="btn btn-sm btn-primary border-0"
                   type="button"
                >
                    <i class="mdi mdi-pencil-outline"></i>
                </a>
            @endcan

        </div>

        {{-- DETAILS --}}
        <div class="small d-flex flex-column gap-2">

            <div class="d-flex align-items-center gap-2">
                <i class="mdi mdi-identifier text-muted"></i>
                <span>#{{ $client->displayId }}</span>
            </div>

            <a wire:navigate href="{{ route('clients.show', $client) }}" class="d-flex align-items-center gap-2">
                <i class="mdi mdi-account text-muted"></i>
                <span>
                    {{ trim(collect([
                        $client->title,
                        $client->name_first,
                        $client->name_middle,
                        $client->name_last
                    ])->filter()->implode(' ')) }}

                    @if($client->name_preferred)
                        ({{ $client->name_preferred }})
                    @endif
                </span>
            </a>

            <div class="d-flex align-items-center gap-2">
                <i class="mdi mdi-cake-variant text-muted"></i>
                <span>
                    {{ $client->date_of_birth?->withAge('d M Y') ?? '—' }}
                    @if($isHighPriority)
                        <i
                                class="mdi mdi-alert-circle text-danger"
                                title="High priority client"
                                data-bs-toggle="tooltip"
                        ></i>
                    @endif
                </span>
            </div>

            <div class="d-flex align-items-center gap-2">
                <i class="mdi mdi-email-outline text-muted"></i>
                @if($client->email_address)
                    <a wire:navigate href="mailto:{{ $client->email_address }}">
                        {{ $client->email_address }}
                    </a>
                @else
                    <span>—</span>
                @endif
            </div>

            <div class="d-flex align-items-center gap-2">
                <i class="mdi mdi-phone-outline text-muted"></i>
                @if($client->phone_number)
                    <a wire:navigate href="tel:{{ preg_replace('/\s+/', '', $client->phone_number) }}">
                        {{ $client->phone_number }}
                    </a>
                @else
                    <span>—</span>
                @endif
            </div>

            @if($client->currentAddress()->exists())
                <div class="d-flex align-items-start gap-2">
                    <i class="mdi mdi-home text-muted"></i>
                    <span>
                        {!! $client->currentAddress->asHtml() !!}
                    </span>
                </div>
            @endif

            @if($client->previousNames->count())
                <div class="d-flex align-items-start gap-2">
                    <i class="mdi mdi-account-switch-outline text-muted mt-1"></i>
                    <div>
                        @foreach($client->previousNames as $previousName)
                            <div>
                                {{ trim(collect([
                                    $previousName->name_first,
                                    $previousName->name_middle,
                                    $previousName->name_last
                                ])->filter()->implode(' ')) }}
                            </div>
                        @endforeach
                    </div>
                </div>
            @endif

            <ul class="list-group list-group-flush">
                @foreach(\App\Models\OptIn::orderBy('display_name')->get() as $optIn)
                    <li class="d-flex align-items-center px-0 bg-transparent border-bottom">

                        @if($client->optIns->contains('id', $optIn->id))
                            <span class="text-success d-flex align-items-center" title="Opted In">
                            <i class="mdi mdi-check-circle fs-5"></i>
                        </span>
                        @else
                            <!-- Show Muted Dash/Cross if Not Opted In -->
                            <span class="text-muted d-flex align-items-center" title="Not Opted In">
                            <i class="mdi mdi-minus-circle-outline fs-5 opacity-50"></i>
                        </span>
                        @endif
                        <span class="small fw-medium text-dark ml-3">
                        {{ $optIn->display_name }}
                    </span>
                    </li>
                @endforeach
            </ul>

        </div>

    </div>
</div>
