<?php
class Article
{
    private $db;
    public function __construct()
    {
        $this->db = new Database;
    }

    public function getAllArticles($limit, $offset): array
{
    $this->db->query("SELECT * FROM article ORDER BY createdAt DESC LIMIT :limit OFFSET :offset");
    
    // Bind values safely to prevent SQL injection
    $this->db->bind(':limit', $limit);
    $this->db->bind(':offset', $offset);
    
    $results = $this->db->resultSet();
    
    return $results ?: []; // Always return an array
}

    public function getRandomArticle()
{
    $this->db->query("SELECT * FROM article ORDER BY RAND() LIMIT 1");
    $result = $this->db->single(); // Fetch just one record

    return $result ?: null; // Return null if no article found
}
    public function getArticleById($articleId)
    {
        $this->db->query("SELECT * FROM article  WHERE id = :articleId");
        $this->db->bind(':articleId', $articleId);
        return $this->db->single();
    }

    public function getTotalArticlesCount(): int
{
    $this->db->query("SELECT COUNT(*) as total FROM article");
    $row = $this->db->single();
    return $row ? (int) $row->total : 0;
}
}