<?php

namespace App\Http\Controllers;

use App\Models\Post;
use Illuminate\Http\Request;
use Inertia\Inertia;
use Illuminate\Support\Facades\Log;

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

        Log::info('Posts data:', ['posts' => $posts->toArray()]); // Debug log

        return Inertia::render('Pengumuman', [
            '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/PegumumanDetail', [
            'article' => $article,
            'relatedArticles' => $relatedArticles
        ]);
    }
}
