<?php

namespace App\Http\Controllers;

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

class UndanganController extends Controller
{
    public function index()
    {
        $posts = Post::with(['kategori', 'subkategori'])
            ->whereHas('kategori', function($query) {
                $query->where('NamaKategori', 'Undangan');
            })
            ->where('IsPublish', true)
            ->orderBy('created_at', 'desc')
            ->get();

        return Inertia::render('Undangan', [
            'posts' => $posts
        ]);
    }

    public function show($slug)
    {
        $article = Post::with(['kategori', 'subkategori'])
            ->where('SlugPost', $slug)
            ->firstOrFail();

        $relatedArticles = Post::with(['kategori', 'subkategori'])
            ->where('KategoriId', $article->KategoriId)
            ->where('PostId', '!=', $article->PostId)
            ->where('IsPublish', true)
            ->latest()
            ->take(5)
            ->get();

        return Inertia::render('detail/UndanganDetail', [
            'article' => $article,
            'relatedArticles' => $relatedArticles
        ]);
    }
}
