<?php
declare(strict_types=1);

require_once __DIR__ . '/../config/database.php';

class BlogPost
{
    public function latest(int $limit = 30): array
    {
        $stmt = db()->prepare('SELECT * FROM blog_posts WHERE status = :status ORDER BY id DESC LIMIT :lim');
        $stmt->bindValue(':status', 'published');
        $stmt->bindValue(':lim', $limit, PDO::PARAM_INT);
        $stmt->execute();
        return $stmt->fetchAll();
    }

    public function all(int $limit = 200): array
    {
        $stmt = db()->prepare('SELECT * FROM blog_posts ORDER BY id DESC LIMIT :lim');
        $stmt->bindValue(':lim', $limit, PDO::PARAM_INT);
        $stmt->execute();
        return $stmt->fetchAll();
    }

    public function create(array $data): bool
    {
        $stmt = db()->prepare('INSERT INTO blog_posts (title,slug,summary,body,cover_image,status) VALUES (:title,:slug,:summary,:body,:cover_image,:status)');
        return $stmt->execute($data);
    }
}
