<?php

declare(strict_types=1);

namespace App\Models;

use App\Core\Model;
use PDO;

/**
 * Announcement Model
 *
 * Handles site-wide announcements data retrieval.
 */
class Announcement extends Model
{
    /**
     * The database table associated with the model.
     */
    protected string $table = 'announcements';

    /**
     * Get all active announcements ordered by created_at DESC.
     *
     * @return array<int, array<string, mixed>>
     */
    public function getActive(): array
    {
        $sql = sprintf(
            'SELECT * FROM `%s` WHERE `active` = :active ORDER BY `created_at` DESC',
            $this->table
        );

        $stmt = $this->db->prepare($sql);
        $stmt->execute([':active' => 1]);

        return $stmt->fetchAll(PDO::FETCH_ASSOC) ?: [];
    }

    /**
     * Get the latest active announcements with a limit.
     *
     * @param int $limit Maximum number of announcements to return
     * @return array<int, array<string, mixed>>
     */
    public function getLatest(int $limit = 5): array
    {
        $sql = sprintf(
            'SELECT * FROM `%s` WHERE `active` = :active ORDER BY `created_at` DESC LIMIT :limit',
            $this->table
        );

        $stmt = $this->db->prepare($sql);
        $stmt->bindValue(':active', 1, PDO::PARAM_INT);
        $stmt->bindValue(':limit', $limit, PDO::PARAM_INT);
        $stmt->execute();

        return $stmt->fetchAll(PDO::FETCH_ASSOC) ?: [];
    }
}
