<?php

namespace App\Http\Controllers;

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

class HomeController extends Controller
{
    public function index()
    {
        $runPosts = Post::with(['kategori', 'subkategori'])
        ->where('IsPublish', true)
        ->orderBy('created_at', 'desc')
        ->get();

        // Fetch Pengumuman
        $pengumuman = Post::with(['kategori', 'subkategori'])
            ->whereHas('kategori', function($query) {
                $query->where('NamaKategori', 'Pengumuman');
            })
            ->where('IsPublish', true)
            ->orderBy('created_at', 'desc')
            ->take(4)
            ->get();

        // Fetch Undangan
        $undangan = Post::with(['kategori', 'subkategori'])
            ->whereHas('kategori', function($query) {
                $query->where('NamaKategori', 'Undangan');
            })
            ->where('IsPublish', true)
            ->orderBy('created_at', 'desc')
            ->take(4)
            ->get();

        // Fetch Peraturan
        $peraturan = Post::with(['kategori', 'subkategori'])
            ->whereHas('kategori', function($query) {
                $query->where('NamaKategori', 'Peraturan');
            })
            ->where('IsPublish', true)
            ->orderBy('created_at', 'desc')
            ->take(4)
            ->get();

        // Fetch Sekilas Info
        $sekilasInfo = Post::with(['kategori', 'subkategori'])
            ->whereHas('kategori', function($query) {
                $query->where('NamaKategori', 'Sekilas Info');
            })
            ->where('IsPublish', true)
            ->orderBy('created_at', 'desc')
            ->take(3)
            ->get();

        return Inertia::render('welcome', [
            'posts' => $pengumuman,
            'undangan' => $undangan,
            'peraturan' => $peraturan,
            'sekilasInfo' => $sekilasInfo,
            'runPosts' => $runPosts,
        ]);
    }
}
