<?php

declare(strict_types=1);

namespace App\Models;

use App\Core\Model;
use PDO;

/**
 * Slider Model
 *
 * Handles homepage carousel/slider data retrieval.
 */
class Slider extends Model
{
    /**
     * The database table associated with the model.
     */
    protected string $table = 'sliders';

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

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

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

    /**
     * Get featured (active) sliders with a limit.
     *
     * @param int $limit Maximum number of sliders to return
     * @return array<int, array<string, mixed>>
     */
    public function getFeatured(int $limit = 5): array
    {
        $sql = sprintf(
            'SELECT * FROM `%s` WHERE `active` = :active ORDER BY `sort_order` ASC 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) ?: [];
    }
}
