<?php

namespace App\Http\Controllers;

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

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

        $articles = News::with('category')
            ->where('author_id', $author->author_id)
            ->orderByDesc('news_id')
            ->paginate(12);

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

        $latestSidebar = News::with('category')
            ->where('author_id', '!=', $author->author_id)
            ->orderByDesc('news_id')
            ->take(5)
            ->get();

        return view('author', compact('author', 'articles', 'totalArticles', 'latestSidebar'));
    }
}
