@extends('layouts.app')

@section('title', $author->name . ' | ' . config('app.name'))
@section('meta_description', Str::limit($author->bio ?: 'Articles by ' . $author->name . ' on ' . config('app.name') . '.', 155))
@section('canonical', route('author.show', $author->slug))
@section('robots', 'index, follow')

@section('schema_json')
<script type="application/ld+json">
{
  "@context": "https://schema.org",
  "@type": "ProfilePage",
  "name": "{{ addslashes($author->name) }}",
  "url": "{{ route('author.show', $author->slug) }}",
  "mainEntity": {
    "@type": "Person",
    "name": "{{ addslashes($author->name) }}",
    "jobTitle": "{{ addslashes($author->designation_english ?? $author->designation ?? '') }}",
    "description": "{{ addslashes(Str::limit($author->bio_english ?? $author->bio ?? '', 200)) }}",
    "worksFor": {
      "@type": "Organization",
      "name": "{{ config('app.name', 'The Hindu') }}"
    }
    @if($author->twitter_url)
    ,"sameAs": ["{{ $author->twitter_url }}"]
    @endif
  }
}
</script>
@endsection

@section('content')

{{-- ══════════════════════════════════════════════════════════════
     AUTHOR HERO HEADER
══════════════════════════════════════════════════════════════ --}}
<div class="author-page">

  <div class="author-hero-band">
    <div class="author-hero-inner">

      {{-- Breadcrumb --}}
      <nav class="author-breadcrumb" aria-label="Breadcrumb">
        <a href="{{ route('index') }}">HOME</a>
        <span class="author-breadcrumb-sep">/</span>
        <span>AUTHORS</span>
        <span class="author-breadcrumb-sep">/</span>
        <span>{{ strtoupper($author->name) }}</span>
      </nav>

      <div class="author-profile-card">

        {{-- Avatar --}}
        <div class="author-avatar-wrap">
          <img
            src="{{ $author->photo_url }}"
            alt="{{ $author->name }}"
            class="author-avatar"
            width="140" height="140"
          >
          @if($author->joined_year)
          <span class="author-since-badge">Since {{ $author->joined_year }}</span>
          @endif
        </div>

        {{-- Info --}}
        <div class="author-profile-info">
          <div class="author-profile-top">
            <h1 class="author-profile-name">{{ $author->name }}</h1>
            @if($author->designation)
            <p class="author-profile-designation">{{ $author->designation }}</p>
            @endif
            @if($author->location)
            <p class="author-profile-location">
              <i class="fa-solid fa-location-dot"></i>&nbsp;{{ $author->location }}
            </p>
            @endif
          </div>

          @if($author->bio)
          <p class="author-profile-bio">{{ $author->bio }}</p>
          @endif

          <div class="author-profile-footer">
            <div class="author-profile-meta">
              <span class="author-stat-pill">
                <i class="fa-regular fa-newspaper"></i>
                {{ number_format($totalArticles) }} {{ Str::plural('Article', $totalArticles) }}
              </span>
              @if($mostRecentDate)
              <span class="author-stat-pill">
                <i class="fa-regular fa-clock"></i>
                Last published {{ $mostRecentDate->format('M j, Y') }}
              </span>
              @endif
            </div>

            <div class="author-profile-social">
              @if($author->twitter_url)
              <a href="{{ $author->twitter_url }}" target="_blank" rel="noopener noreferrer" aria-label="Twitter / X" class="author-social-btn">
                <i class="fa-brands fa-x-twitter"></i>
              </a>
              @endif
              @if($author->facebook_url)
              <a href="{{ $author->facebook_url }}" target="_blank" rel="noopener noreferrer" aria-label="Facebook" class="author-social-btn">
                <i class="fa-brands fa-facebook-f"></i>
              </a>
              @endif
              @if($author->linkedin_url)
              <a href="{{ $author->linkedin_url }}" target="_blank" rel="noopener noreferrer" aria-label="LinkedIn" class="author-social-btn">
                <i class="fa-brands fa-linkedin-in"></i>
              </a>
              @endif
            </div>
          </div>
        </div>

      </div>{{-- /.author-profile-card --}}
    </div>{{-- /.author-hero-inner --}}
  </div>{{-- /.author-hero-band --}}


  {{-- ══════════════════════════════════════════════════════════════
       CATEGORY FILTER TABS
  ══════════════════════════════════════════════════════════════ --}}
  @if($authorCategories->isNotEmpty())
  <div class="author-filter-bar">
    <div class="author-filter-inner">
      <a href="{{ route('author.show', $author->slug) }}"
         class="author-filter-tab {{ !$activeCategorySlug ? 'active' : '' }}">
        All <span class="author-filter-count">{{ $totalArticles }}</span>
      </a>
      @foreach($authorCategories as $ac)
      <a href="{{ route('author.show', ['slug' => $author->slug, 'category' => $ac->slug]) }}"
         class="author-filter-tab {{ $activeCategorySlug === $ac->slug ? 'active' : '' }}">
        {{ $ac->category_name }}
        <span class="author-filter-count">{{ $ac->articles_count }}</span>
      </a>
      @endforeach
    </div>
  </div>
  @endif


  {{-- ══════════════════════════════════════════════════════════════
       MAIN CONTENT + SIDEBAR
  ══════════════════════════════════════════════════════════════ --}}
  <div class="author-body-layout">

    {{-- ── MAIN ARTICLES COLUMN ──────────────────────────────── --}}
    <div class="author-articles-col">

      {{-- Section heading --}}
      <div class="author-section-heading">
        <span class="author-section-line"></span>
        <h2>{{ $activeCategorySlug ? strtoupper($activeCategoryLabel) : 'ALL ARTICLES' }}</h2>
        <span class="author-section-line"></span>
      </div>

      {{-- Featured first article (page 1 only) --}}
      @if($articles->count() > 0 && $articles->currentPage() === 1)
        @php $featured = $articles->first(); @endphp
        <article class="author-featured-article" itemscope itemtype="https://schema.org/NewsArticle">
          <a href="{{ $featured->canonical_url }}" class="author-feat-img-link" tabindex="-1">
            <div class="author-feat-img-wrap">
              <img
                src="{{ $featured->photo_url }}"
                alt="{{ $featured->img_alt ?: $featured->news_title }}"
                class="author-feat-img"
                itemprop="image"
                loading="eager"
              >
            </div>
          </a>
          <div class="author-feat-body">
            <span class="author-feat-tag">
              <a href="{{ route('category.show', $featured->category->slug ?? 'news') }}">
                {{ strtoupper($featured->category->category_name ?? 'NEWS') }}
              </a>
            </span>
            <h2 class="author-feat-title" itemprop="headline">
              <a href="{{ $featured->canonical_url }}" title="{{ $featured->news_title }}">
                {{ $featured->news_title }}
              </a>
            </h2>
            <p class="author-feat-summary" itemprop="description">
              {{ Str::limit(strip_tags($featured->news_content_short), 220) }}
            </p>
            <div class="author-feat-meta">
              @if($featured->published_at)
              <time datetime="{{ $featured->published_at->toIso8601String() }}" itemprop="datePublished">
                {{ $featured->published_at->format('F j, Y') }}
              </time>
              @endif
              <span class="author-feat-readtime">
                <i class="fa-regular fa-clock"></i> {{ $featured->reading_time }} min read
              </span>
            </div>
          </div>
        </article>
      @endif

      {{-- Rest of article list --}}
      <div class="author-article-list">
        @php $startIdx = ($articles->currentPage() === 1) ? 1 : 0; @endphp
        @forelse($articles->slice($startIdx) as $art)
        <article class="author-article-item" itemscope itemtype="https://schema.org/NewsArticle">
          <a href="{{ $art->canonical_url }}" class="author-article-img-link" tabindex="-1" aria-hidden="true">
            <img
              src="{{ $art->photo_url }}"
              alt="{{ $art->img_alt ?: $art->news_title }}"
              class="author-article-img"
              itemprop="image"
              loading="lazy"
              width="220" height="145"
            >
          </a>
          <div class="author-article-body">
            <span class="author-article-tag">
              <a href="{{ route('category.show', $art->category->slug ?? 'news') }}">
                {{ strtoupper($art->category->category_name ?? 'NEWS') }}
              </a>
            </span>
            <h3 class="author-article-title" itemprop="headline">
              <a href="{{ $art->canonical_url }}" title="{{ $art->news_title }}">
                {{ $art->news_title }}
              </a>
            </h3>
            <p class="author-article-summary">
              {{ Str::limit(strip_tags($art->news_content_short), 130) }}
            </p>
            <div class="author-article-meta">
              @if($art->published_at)
              <time datetime="{{ $art->published_at->toIso8601String() }}" itemprop="datePublished">
                {{ $art->published_at->format('M j, Y') }}
              </time>
              @endif
              <span class="author-article-readtime">
                <i class="fa-regular fa-clock"></i> {{ $art->reading_time }} min
              </span>
            </div>
          </div>
        </article>
        @empty
        <p class="author-empty-msg">No articles found.</p>
        @endforelse
      </div>

      {{-- Pagination --}}
      @if($articles->hasPages())
      <div class="hindu-pagination">
        {{ $articles->appends(request()->query())->links('partials.pagination') }}
      </div>
      @endif

    </div>{{-- /.author-articles-col --}}


    {{-- ── SIDEBAR ────────────────────────────────────────────── --}}
    <aside class="author-sidebar" aria-label="Sidebar">

      {{-- Other authors --}}
      @if($otherAuthors->isNotEmpty())
      <div class="sidebar-block">
        <div class="sidebar-title">More Authors</div>
        @foreach($otherAuthors as $oa)
        <a href="{{ route('author.show', $oa->slug) }}" class="author-sidebar-author-item">
          <img
            src="{{ $oa->photo_url }}"
            alt="{{ $oa->name }}"
            class="author-sidebar-author-img"
            loading="lazy"
            width="44" height="44"
          >
          <div class="author-sidebar-author-info">
            <span class="author-sidebar-author-name">{{ $oa->name }}</span>
            <span class="author-sidebar-author-role">{{ $oa->designation }}</span>
          </div>
        </a>
        @endforeach
      </div>
      @endif

      {{-- Latest news sidebar --}}
      @if($latestSidebar->isNotEmpty())
      <div class="sidebar-block">
        <div class="sidebar-title">Latest News</div>
        @foreach($latestSidebar as $ls)
        <div class="sidebar-item">
          <img
            src="{{ $ls->photo_url }}"
            alt="{{ $ls->news_title }}"
            loading="lazy"
            width="70" height="52"
          >
          <div>
            <span class="sidebar-cat">{{ strtoupper($ls->category->category_name ?? '') }}</span>
            <p class="sidebar-text">
              <a href="{{ $ls->canonical_url }}">{{ Str::limit($ls->news_title, 80) }}</a>
            </p>
          </div>
        </div>
        @endforeach
      </div>
      @endif

      {{-- Popular categories --}}
      @if($popularCategories->isNotEmpty())
      <div class="sidebar-block">
        <div class="sidebar-title">Popular Topics</div>
        @foreach($popularCategories as $pc)
        <a href="{{ route('category.show', $pc->slug) }}" class="sidebar-cat-link">
          {{ $pc->category_name }}
          <span class="sidebar-cat-count">{{ number_format($pc->news_count) }}</span>
        </a>
        @endforeach
      </div>
      @endif

    </aside>{{-- /.author-sidebar --}}

  </div>{{-- /.author-body-layout --}}
</div>{{-- /.author-page --}}

@endsection
