<?php

namespace App\Http\Controllers\Public;

use App\Http\Controllers\Controller;
use App\Models\BlogPost;
use App\Models\BlogCategory;
use Illuminate\Http\Request;

class BlogController extends Controller
{
    public function index()
    {
        $posts = BlogPost::where('is_published', true)->latest('published_at')->paginate(9);
        $categories = BlogCategory::all();
        return view('public.blog.index', compact('posts', 'categories'));
    }

    public function show($slug)
    {
        $post = BlogPost::where('slug', $slug)->where('is_published', true)->firstOrFail();
        return view('public.blog.show', compact('post'));
    }

    public function category($slug)
    {
        $category = BlogCategory::where('slug', $slug)->firstOrFail();
        $posts = $category->posts()->where('is_published', true)->latest('published_at')->paginate(9);
        $categories = BlogCategory::all();
        return view('public.blog.index', compact('posts', 'categories', 'category'));
    }
}
