<?php

namespace App\Http\Controllers;

use App\Models\Author;
use App\Models\Category;
use App\Models\News;

class AuthorController extends Controller
{
    public function show(string $slug)
    {
        // ── Fetch author ───────────────────────────────────────────────────────
        $author = Author::where('slug', $slug)->firstOrFail();

        // ── Category filter (optional ?category=slug query param) ─────────────
        $activeCategorySlug  = request('category');
        $activeCategoryLabel = null;

        // ── Articles query ─────────────────────────────────────────────────────
        $articlesQuery = News::with(['category'])
            ->where('author_id', $author->author_id)
            ->published()
            ->orderByDesc('news_id');

        if ($activeCategorySlug) {
            $activeCategory = Category::where('slug', $activeCategorySlug)->first();
            if ($activeCategory) {
                $articlesQuery->where('category_id', $activeCategory->category_id);
                $activeCategoryLabel = $activeCategory->category_name;
            }
        }

        $articles = $articlesQuery->paginate(9);

        // ── Stats ──────────────────────────────────────────────────────────────
        $totalArticles = $author->news()->published()->count();

        $mostRecentDate = $author->news()
            ->published()
            ->orderByDesc('published_at')
            ->value('published_at');
        $mostRecentDate = $mostRecentDate ? \Carbon\Carbon::parse($mostRecentDate) : null;

        // ── Categories this author has written in (for filter tabs) ────────────
        $authorCategories = Category::withCount(['news as articles_count' => function ($q) use ($author) {
                $q->where('author_id', $author->author_id)->published();
            }])
            ->having('articles_count', '>', 0)
            ->orderByDesc('articles_count')
            ->get();

        // ── Sidebar: other authors (excluding current) ─────────────────────────
        $otherAuthors = Author::where('author_id', '!=', $author->author_id)
            ->withCount('news')
            ->orderByDesc('news_count')
            ->take(5)
            ->get();

        // ── Sidebar: latest news from other authors ────────────────────────────
        $latestSidebar = News::with('category')
            ->published()
            ->where('author_id', '!=', $author->author_id)
            ->orderByDesc('news_id')
            ->take(5)
            ->get();

        // ── Sidebar: popular categories ────────────────────────────────────────
        $popularCategories = Category::withCount(['news as news_count' => function ($q) {
                $q->published();
            }])
            ->having('news_count', '>', 0)
            ->orderByDesc('news_count')
            ->take(6)
            ->get();

        return view('author', compact(
            'author',
            'articles',
            'totalArticles',
            'mostRecentDate',
            'authorCategories',
            'activeCategorySlug',
            'activeCategoryLabel',
            'otherAuthors',
            'latestSidebar',
            'popularCategories'
        ));
    }
}
