@extends('layouts.app')
@section('title', 'Search')
@section('breadcrumb')
    <span style="font-weight:600;color:var(--text-primary);">Search Results</span>
@endsection

@section('content')
    <div class="page-header">
        <h1>Search Results</h1>
    </div>

    @if($query)
        <p class="text-muted mb-4">Results for "<strong>{{ $query }}</strong>"</p>
    @endif

    {{-- Projects --}}
    @if(count($results['projects']))
        <div class="card mb-4">
            <div class="card-header">
                <span class="card-header-title">📁 Projects ({{ count($results['projects']) }})</span>
            </div>
            <div>
                @foreach($results['projects'] as $project)
                    <a href="{{ route('projects.show', $project) }}" class="task-item" style="text-decoration:none;color:var(--text-primary);">
                        <div style="width:8px;height:8px;border-radius:50%;background:{{ $project->color }};"></div>
                        <div class="task-info">
                            <div class="task-title">{{ $project->name }}</div>
                            <div class="text-xs text-muted">{{ Str::limit($project->description, 80) }}</div>
                        </div>
                        <span class="badge badge-{{ $project->status_badge }}">{{ ucfirst(str_replace('_', ' ', $project->status)) }}</span>
                    </a>
                @endforeach
            </div>
        </div>
    @endif

    {{-- Tasks --}}
    @if(count($results['tasks']))
        <div class="card mb-4">
            <div class="card-header">
                <span class="card-header-title">✅ Tasks ({{ count($results['tasks']) }})</span>
            </div>
            <div>
                @foreach($results['tasks'] as $task)
                    <a href="{{ route('tasks.show', $task) }}" class="task-item" style="text-decoration:none;color:var(--text-primary);">
                        <div class="task-checkbox {{ $task->is_completed ? 'completed' : '' }}"></div>
                        <div class="task-info">
                            <div class="task-title">{{ $task->title }}</div>
                            <div class="task-meta">
                                <span class="task-meta-item">{{ $task->project->name ?? '' }}</span>
                                @if($task->assignee)
                                    <span class="task-meta-item">{{ $task->assignee->name }}</span>
                                @endif
                            </div>
                        </div>
                        <span class="badge badge-{{ $task->status === 'completed' ? 'success' : 'primary' }}">{{ ucfirst(str_replace('_', ' ', $task->status)) }}</span>
                    </a>
                @endforeach
            </div>
        </div>
    @endif

    {{-- Users --}}
    @if(count($results['users']))
        <div class="card mb-4">
            <div class="card-header">
                <span class="card-header-title">👤 People ({{ count($results['users']) }})</span>
            </div>
            <div>
                @foreach($results['users'] as $user)
                    <div class="task-item">
                        <div class="avatar avatar-sm">{{ $user->initials }}</div>
                        <div class="task-info">
                            <div class="task-title">{{ $user->name }}</div>
                            <div class="text-xs text-muted">{{ $user->email }} · {{ $user->job_title ?? '' }}</div>
                        </div>
                    </div>
                @endforeach
            </div>
        </div>
    @endif

    {{-- Comments --}}
    @if(count($results['comments']))
        <div class="card mb-4">
            <div class="card-header">
                <span class="card-header-title">💬 Comments ({{ count($results['comments']) }})</span>
            </div>
            <div>
                @foreach($results['comments'] as $comment)
                    <div class="task-item">
                        <div class="avatar avatar-xs">{{ $comment->user->initials }}</div>
                        <div class="task-info">
                            <div class="task-title">{{ Str::limit($comment->body, 80) }}</div>
                            <div class="text-xs text-muted">
                                on <a href="{{ route('tasks.show', $comment->task) }}">{{ $comment->task->title }}</a>
                                in {{ $comment->task->project->name ?? '' }}
                            </div>
                        </div>
                    </div>
                @endforeach
            </div>
        </div>
    @endif

    @if(!count($results['projects']) && !count($results['tasks']) && !count($results['users']) && !count($results['comments']))
        <div class="empty-state">
            <div class="empty-state-icon">
                <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><circle cx="11" cy="11" r="8"></circle><line x1="21" y1="21" x2="16.65" y2="16.65"></line></svg>
            </div>
            <div class="empty-state-title">{{ $query ? 'No results found' : 'Start searching' }}</div>
            <div class="empty-state-text">{{ $query ? 'Try different keywords.' : 'Use the search bar to find projects, tasks, and people.' }}</div>
        </div>
    @endif
@endsection
