<?php

namespace App\Http\Controllers;

use App\Models\Post;
use Illuminate\Http\Request;
use Inertia\Inertia;

class TagController extends Controller
{
    public function show($tag)
    {
        // Fetch posts that contain the specific tag in their JSON 'tags' column
        $posts = Post::with(['user:id,name,avatar', 'category:id,name,slug'])
            ->where('status', 'published')
            ->whereJsonContains('tags', $tag)
            ->latest()
            ->paginate(12) 
            ->withQueryString();

        return Inertia::render('Tag/Show', [
            'tag' => $tag,
            'initialPosts' => $posts,
        ]);
    }
}