<div class="tab-pane fade p-4" id="invitedMeetingTab" role="tabpanel">
    <h5 class="fw-bold mb-4">Invited Meetings</h5>
    @forelse ($invitedMeetings as $meeting)
        <div class="border rounded p-3 mb-2">
            <strong>{{ $meeting->title ?: $meeting->subject ?: 'Meeting' }}</strong>
            <span class="badge bg-soft-primary text-primary ms-2">{{ $meeting->meeting_type ?: 'Meeting' }}</span>
            <div class="text-muted">{{ $meeting->start_date }} {{ $meeting->start_time }} · {{ $meeting->location ?: 'No location' }}</div>
            @if ($meeting->agenda)<div class="mt-1">{{ $meeting->agenda }}</div>@endif
        </div>
    @empty
        <p class="text-muted">No invited meetings are linked to this Lead.</p>
    @endforelse
</div>

<div class="tab-pane fade p-4" id="projectsTab" role="tabpanel">
    <h5 class="fw-bold mb-4">Lead Projects</h5>
    @forelse ($projects as $project)
        <div class="border rounded p-3 mb-2 d-flex justify-content-between">
            <div><strong>{{ $project->name }}</strong><div class="text-muted">{{ $project->description }}</div></div>
            <div><span class="badge bg-soft-primary text-primary">{{ $project->status }}</span>
                <div class="small mt-1">{{ $project->progress }}% complete</div></div>
        </div>
    @empty
        <p class="text-muted">No Projects are linked directly or through converted Deals.</p>
    @endforelse
</div>

<div class="tab-pane fade p-4" id="tradeDetailsTab" role="tabpanel">
    <h5 class="fw-bold mb-4">Trade Details</h5>
    <div class="table-responsive"><table class="table">
        <thead><tr><th>Document</th><th>Type</th><th>Deal</th><th>Status</th><th>Total</th></tr></thead>
        <tbody>
        @forelse ($tradeDocuments as $document)
            <tr><td>{{ $document->document_number }}</td><td>{{ ucfirst($document->type) }}</td>
                <td>{{ $document->deal?->name ?: '#'.$document->deal_id }}</td><td>{{ $document->status }}</td>
                <td>{{ $document->currency }} {{ number_format((float) $document->total, 2) }}</td></tr>
        @empty
            <tr><td colspan="5" class="text-muted">No trade documents exist for this Lead’s Deals.</td></tr>
        @endforelse
        </tbody>
    </table></div>
</div>

<div class="tab-pane fade p-4" id="casesTab" role="tabpanel">
    <div class="d-flex justify-content-between mb-4"><h5 class="fw-bold mb-0">Lead Cases</h5><a class="btn btn-sm btn-primary" href="{{ route('cases.index',['lead_id'=>$lead->id]) }}">Create Case</a></div>
    @forelse ($cases as $case)
        <div class="border rounded p-3 mb-2"><strong>{{ $case->case_number }} · {{ $case->subject }}</strong>
            <span class="badge bg-soft-warning text-warning ms-2">{{ $case->priority }}</span>
            <span class="badge bg-soft-primary text-primary ms-1">{{ $case->status }}</span>
            <div class="text-muted mt-1">{{ $case->description }}</div><a href="{{ route('cases.show',$case) }}">Open Case</a></div>
    @empty
        <p class="text-muted">No Cases are assigned to this Lead.</p>
    @endforelse
</div>

<div class="tab-pane fade p-4" id="surveyTab" role="tabpanel">
    <h5 class="fw-bold mb-4">Lead Surveys</h5>
    @forelse ($surveys as $survey)
        @php($surveyRecipient = $survey->recipients()->where('respondent_type', $lead::class)->where('respondent_id', $lead->id)->first())
        <div class="border rounded p-3 mb-2"><strong>{{ $survey->name }}</strong>
            <span class="badge bg-soft-primary text-primary ms-2">{{ $survey->type }}</span>
            <span class="badge bg-soft-success text-success ms-1">{{ $survey->status }}</span>
            <div class="text-muted mt-1">{{ $survey->description }}</div><a href="{{ route('surveys.show',$survey) }}">Analytics</a>@if($surveyRecipient) · <a href="{{ route('surveys.respond',$surveyRecipient->token) }}">{{ ucfirst($surveyRecipient->status) }}</a>@endif</div>
    @empty
        <p class="text-muted">No Surveys target this Lead.</p>
    @endforelse
</div>

<div class="tab-pane fade p-4" id="conversionTab" role="tabpanel">
    <div class="row g-3 mb-4">
        <div class="col-md-6"><div class="border rounded p-3">
            <small class="text-muted">Last Interaction</small>
            <div class="fw-bold">{{ $lastInteraction?->title ?: $lastInteraction?->subject ?: 'None' }}</div>
            <div>{{ $lastInteraction?->start_date ?: $lastInteraction?->created_at?->format('M d, Y H:i') }}</div>
        </div></div>
        <div class="col-md-6"><div class="border rounded p-3">
            <small class="text-muted">Next Interaction</small>
            <div class="fw-bold">{{ $nextInteraction?->title ?: $nextInteraction?->subject ?: 'None scheduled' }}</div>
            <div>{{ $nextInteraction?->start_date }} {{ $nextInteraction?->start_time }}</div>
        </div></div>
    </div>
    @if ((int) $lead->stage_id === 7)
        <div class="alert alert-warning"><strong>Unqualified reason:</strong> {{ $lead->unqualified_reason }}</div>
    @endif
    @if ((int) $lead->stage_id === 6 && !$lead->moved_at)
        <a class="btn btn-primary mb-4" href="{{ route('leads.conversion-review', $lead->id) }}">
            Start guided Deal conversion
        </a>
    @endif
    @if ($lead->moved_at)
        <div class="alert alert-success">Converted to Deal #{{ $lead->moved_to_id }} on {{ $lead->moved_at->format('M d, Y H:i') }}.</div>
    @endif
    <h5 class="fw-bold">Conversion History</h5>
    @forelse ($conversionHistories as $history)
        <div class="border-start border-3 border-success ps-3 pb-4">
            <strong>Converted to {{ $history->deal?->name ?: 'Deal #'.$history->deal_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['project_ids'] ?? []) }} Projects,
                {{ count($history->relationship_snapshot['case_ids'] ?? []) }} Cases.
            </div>
            @if (count($history->duplicate_matches ?? []))
                <div class="text-warning">Duplicate Deal candidates reviewed: #{{ implode(', #', $history->duplicate_matches) }}</div>
            @endif
        </div>
    @empty
        <p class="text-muted">No conversion has been completed.</p>
    @endforelse
    <x-profile-notification-history entity-type="Lead" :entity-id="$lead->id" />
</div>

<div class="tab-pane fade p-4" id="filesTab" role="tabpanel">
    <x-crm-file-manager module="leads" :record="$lead" />
</div>
