<?php

namespace App\Controllers;

use App\Core\Controller;

class BlogController extends Controller {
    
    public function index() {
        $db = db_pdo();
        $posts = $db->query("SELECT * FROM posts WHERE status = 'published' ORDER BY created_at DESC")->fetchAll();
        $this->view('blog/index', ['posts' => $posts]);
    }

    public function detail($params) {
        $slug = $params['slug'];
        $db = db_pdo();
        $stmt = $db->prepare("SELECT * FROM posts WHERE slug = ? AND status = 'published'");
        $stmt->execute([$slug]);
        $post = $stmt->fetch();

        if (!$post) {
            $this->view('404');
            return;
        }

        $this->view('blog/detail', ['post' => $post]);
    }
}
