<?php

namespace App\Http\Controllers;

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

class SearchController extends Controller
{
    public function index(Request $request)
    {
        $query = $request->input('q');

        $searchResults = Post::with(['kategori', 'subkategori'])
            ->where('IsPublish', true)
            ->where(function($q) use ($query) {
                $q->where('JudulPost', 'ILIKE', "%{$query}%")
                  ->orWhere('DescPost', 'ILIKE', "%{$query}%");
            })
            ->latest()
            ->get();

        return Inertia::render('Search', [
            'searchResults' => $searchResults,
            'searchQuery' => $query
        ]);
    }
}
