@include('components.profile-overview-styles')
@php
    $contactCompletedActivities = $activities->filter(fn ($activity) => in_array(strtolower((string) ($activity->lifecycle_status ?: $activity->status)), ['completed', 'done'], true))->count();
    $contactActivityProgress = $activities->isNotEmpty() ? (int) round($contactCompletedActivities / $activities->count() * 100) : 0;
    $contactEmail = $contact->contactEmails->first()?->email;
    $contactPhone = $contact->contactPhones->first()?->phone;
    $contactLanguages = $contact->contactLanguages->pluck('language.name')->filter()->join(', ');
    $contactLastActivity = $activityTimeline->sortByDesc('occurred_at')->first();
    $contactNextActivity = $activityTimeline->filter(fn ($item) => $item['occurred_at']?->isFuture() && !in_array($item['status'], ['completed', 'cancelled'], true))->sortBy('occurred_at')->first();
@endphp

<div class="pro-overview" data-testid="contact-profile-overview">
    <div class="d-flex flex-wrap align-items-center justify-content-between gap-3 mb-4">
        <div><h5 class="fw-bold mb-1">Contact Overview</h5><p class="text-muted mb-0">Contact details, engagement, and connected CRM records.</p></div>
        <div class="d-flex gap-2">
            <a href="{{ route('related-activity.index', ['contacts', $contact->id]) }}" class="btn btn-sm btn-outline-primary"><i class="feather-activity me-1"></i>Activity Timeline</a>
            @can('update_contacts')
                <a href="{{ route('contacts.edit', ['id' => $contact->id]) }}" class="btn btn-sm btn-primary"><i class="feather-edit-3 me-1"></i>Edit Contact</a>
            @endcan
        </div>
    </div>

    <div class="row g-3 mb-4">
        @foreach ([
            ['icon' => 'feather-activity', 'tone' => 'primary', 'value' => $activities->count(), 'label' => 'Activities', 'hint' => $contactCompletedActivities.' completed'],
            ['icon' => 'feather-target', 'tone' => 'warning', 'value' => $leads->count(), 'label' => 'Linked Leads', 'hint' => 'Lead relationships'],
            ['icon' => 'feather-briefcase', 'tone' => 'success', 'value' => $deals->count(), 'label' => 'Linked Deals', 'hint' => 'Sales opportunities'],
            ['icon' => 'feather-dollar-sign', 'tone' => 'info', 'value' => number_format((float) $totalDealValue, 2), 'label' => 'Deal Value', 'hint' => 'Combined value'],
        ] as $metric)
            <div class="col-sm-6 col-xl-3"><div class="pro-metric"><div class="d-flex align-items-center justify-content-between mb-3"><span class="pro-icon bg-soft-{{ $metric['tone'] }} text-{{ $metric['tone'] }}"><i class="{{ $metric['icon'] }}"></i></span><strong class="fs-5">{{ $metric['value'] }}</strong></div><div class="fw-semibold">{{ $metric['label'] }}</div><small class="text-muted">{{ $metric['hint'] }}</small></div></div>
        @endforeach
    </div>

    <div class="row g-4 mb-4">
        <div class="col-xl-6"><section class="pro-panel"><div class="pro-panel-header"><span class="pro-icon bg-soft-primary text-primary"><i class="feather-user"></i></span><div><h6 class="fw-bold mb-0">Contact Information</h6><small class="text-muted">Identity and primary channels</small></div></div><div class="pro-panel-body">
            @foreach ([['Full Name', trim($contact->first_name.' '.$contact->last_name) ?: 'Not provided'], ['Email', $contactEmail ?: 'Not provided'], ['Phone', $contactPhone ?: 'Not provided'], ['Job Title', $contact->job_title ?: 'Not provided'], ['Company', $contact->company?->name ?: 'Not provided'], ['Department', $contact->department ?: 'Not provided'], ['Languages', $contactLanguages ?: 'Not provided']] as [$label, $value])
                <div class="pro-detail"><span class="pro-label">{{ $label }}</span><span class="pro-value">{{ $value }}</span></div>
            @endforeach
        </div></section></div>
        <div class="col-xl-6"><section class="pro-panel"><div class="pro-panel-header"><span class="pro-icon bg-soft-success text-success"><i class="feather-info"></i></span><div><h6 class="fw-bold mb-0">Contact Details</h6><small class="text-muted">Classification and ownership</small></div></div><div class="pro-panel-body">
            @foreach ([['Stage', $contact->stage?->name ?: 'Not set'], ['Record Status', ucfirst($contact->status ?: 'active')], ['Owner', $contact->assignedUser?->name ?: 'Not assigned'], ['Type', ucfirst($contact->type ?: 'Not set')], ['Priority', ucfirst($contact->priority ?: 'Not set')], ['Gender', ucfirst($contact->gender ?: 'Not provided')], ['Date of Birth', $contact->date_of_birth ?: 'Not provided'], ['Created', $contact->created_at?->format('d M Y, h:i A') ?: '—']] as [$label, $value])
                <div class="pro-detail"><span class="pro-label">{{ $label }}</span><span class="pro-value">{{ $value }}</span></div>
            @endforeach
        </div></section></div>
    </div>

    <div class="row g-4">
        <div class="col-xl-7"><section class="pro-panel"><div class="pro-panel-header"><span class="pro-icon bg-soft-warning text-warning"><i class="feather-clock"></i></span><div><h6 class="fw-bold mb-0">Activity Snapshot</h6><small class="text-muted">{{ $contactActivityProgress }}% of activities completed</small></div></div><div class="pro-panel-body py-3"><div class="row g-3">
            <div class="col-md-6"><div class="pro-snapshot"><i class="feather-check-circle"></i><small class="text-muted d-block">Last interaction</small><strong>{{ $contactLastActivity['title'] ?? 'No activity yet' }}</strong>@if($contactLastActivity)<small class="text-muted d-block">{{ $contactLastActivity['occurred_at']->format('d M Y, h:i A') }}</small>@endif</div></div>
            <div class="col-md-6"><div class="pro-snapshot"><i class="feather-calendar"></i><small class="text-muted d-block">Next interaction</small><strong>{{ $contactNextActivity['title'] ?? 'Nothing scheduled' }}</strong>@if($contactNextActivity)<small class="text-muted d-block">{{ $contactNextActivity['occurred_at']->format('d M Y, h:i A') }}</small>@endif</div></div>
        </div></div></section></div>
        <div class="col-xl-5"><section class="pro-panel"><div class="pro-panel-header"><span class="pro-icon bg-soft-info text-info"><i class="feather-map-pin"></i></span><div><h6 class="fw-bold mb-0">Location</h6><small class="text-muted">Primary address details</small></div></div><div class="pro-panel-body">
            <div class="pro-detail"><span class="pro-label">Address</span><span class="pro-value">{{ collect([$contact->address_1, $contact->address_2])->filter()->join(', ') ?: 'Not provided' }}</span></div>
            <div class="pro-detail"><span class="pro-label">City / Province</span><span class="pro-value">{{ collect([$contact->city, $contact->province])->filter()->join(', ') ?: 'Not provided' }}</span></div>
            <div class="pro-detail"><span class="pro-label">Country</span><span class="pro-value">{{ $contact->country?->name ?: 'Not provided' }}</span></div>
            <div class="pro-detail"><span class="pro-label">Zip Code</span><span class="pro-value">{{ $contact->zip_code ?: 'Not provided' }}</span></div>
        </div></section></div>
    </div>
</div>
