<?php

namespace App\Modules\Frontend\Controllers;

use App\Modules\Frontend\Models\BlogModel;
use CodeIgniter\Controller;

class BlogController extends Controller
{
    public function index()
    {
        $model = model(BlogModel::class);
        $posts = $model->where('status', 'published')
                       ->orderBy('created_at', 'DESC')
                       ->findAll();

        return view('App\Modules\Frontend\Views\blog\index', ['blogPosts' => $posts]);
    }

    public function show(string $slug = null)
    {
        if (!$slug) {
            throw \CodeIgniter\Exceptions\PageNotFoundException::forPageNotFound();
        }

        $model = model(BlogModel::class);
        $post = $model->where('slug', $slug)
                      ->where('status', 'published')
                      ->first();

        if (!$post) {
            throw \CodeIgniter\Exceptions\PageNotFoundException::forPageNotFound();
        }

        return view('App\Modules\Frontend\Views\blog\show', ['post' => $post]);
    }
}