@extends('layouts.app')

@section('title', $query ? 'Search results for "' . $query . '" | Business Standard' : 'Search | Business Standard')
@section('robots', 'noindex, follow')

@section('content')
<div class="mt-6">

  {{-- BREADCRUMB --}}
  <div class="text-sm text-gray-500 mb-4">
    <a href="{{ route('index') }}" class="text-red-600 hover:underline">Home</a> / Search
  </div>

  {{-- SEARCH FORM --}}
  <div class="border-b pb-4 mb-6">
    <form action="{{ route('search') }}" method="GET" class="flex gap-3">
      <input type="text" name="q" value="{{ $query }}" placeholder="Search Business Standard..."
        class="flex-1 border border-gray-300 rounded px-4 py-2 text-sm focus:outline-none focus:border-red-600"/>
      <button type="submit" class="bg-red-700 text-white px-6 py-2 rounded font-semibold hover:bg-red-800 transition">Search</button>
    </form>
  </div>

  @if($query)
  <h2 class="text-xl font-bold mb-2">
    Search results for: <span class="text-red-700">"{{ $query }}"</span>
  </h2>
  <p class="text-sm text-gray-500 mb-6">{{ $results->total() }} results found</p>

  @if($results->isNotEmpty())
  <div class="space-y-6">
    @foreach($results as $article)
    <a href="{{ $article->canonical_url }}" class="flex gap-4 border-b pb-6 hover:text-red-700 transition">
      <img src="{{ $article->photo_url }}" class="w-32 h-24 object-cover rounded flex-shrink-0" alt="{{ $article->img_alt_english ?? $article->news_title }}"/>
      <div class="flex-1">
        <div class="flex items-center gap-2 mb-1">
          @if($article->category)
          <span class="text-xs text-red-600 font-semibold uppercase">{{ $article->category->category_name_english ?? $article->category->category_name }}</span>
          @endif
          <span class="text-xs text-gray-400">·</span>
          <span class="text-xs text-gray-500">{{ $article->published_at ? $article->published_at->format('M j, Y') : '' }}</span>
        </div>
        <h3 class="text-lg font-bold leading-snug mb-1">{{ $article->news_title_english ?? $article->news_title }}</h3>
        <p class="text-sm text-gray-600">{{ Str::limit(strip_tags($article->news_content_short_english ?? $article->news_content_short ?? ''), 200) }}</p>
        <div class="mt-2 text-xs text-gray-500">
          {{ $article->reading_time }} min read
          @if($article->author) · By {{ $article->author->name }} @endif
        </div>
      </div>
    </a>
    @endforeach
  </div>

  {{-- PAGINATION --}}
  @if($results->hasPages())
  <div class="mt-8 flex justify-center">
    {{ $results->appends(['q' => $query])->links() }}
  </div>
  @endif

  @else
  <div class="text-center py-16">
    <p class="text-gray-500 text-lg">No results found for <strong>"{{ $query }}"</strong></p>
    <p class="text-gray-400 text-sm mt-2">Try different keywords or browse our sections below.</p>
    <div class="mt-6 flex flex-wrap gap-3 justify-center">
      @foreach($allCategories as $cat)
      <a href="{{ route('category.show', $cat->slug) }}" class="px-4 py-2 border border-red-600 text-red-600 rounded-full text-sm hover:bg-red-600 hover:text-white transition">
        {{ $cat->category_name_english ?? $cat->category_name }}
      </a>
      @endforeach
    </div>
  </div>
  @endif

  @else
  <div class="text-center py-16">
    <p class="text-gray-400">Enter a search term to find articles.</p>
  </div>
  @endif

</div>
@endsection
