<?php

namespace App\Models;

use App\Core\Model;

class Page extends Model
{
    protected static string $table = 'iw_pages';

    public static function published(string $orderBy = 'sort_order', string $direction = 'ASC'): array
    {
        return static::where(['status' => 'published'], $orderBy, $direction);
    }

    public static function homepage(): ?array
    {
        return static::findWhere(['is_homepage' => 1, 'status' => 'published']);
    }

    /**
     * Most recently updated pages for the admin dashboard (lightweight).
     */
    public static function recentlyUpdated(int $limit = 5): array
    {
        $limit = max(1, min(50, $limit));
        return static::db()->fetchAll(
            "SELECT id, title, slug, status, updated_at
             FROM iw_pages
             ORDER BY updated_at DESC
             LIMIT {$limit}"
        );
    }
}
