<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Storage;

/**
 * لقطات شاشة التطبيق في الصفحة الرئيسية للموقع.
 *
 * @property int $id
 * @property string $image_path
 * @property string|null $caption
 * @property int $sort_order
 * @property bool $is_active
 */
class LandingScreenshot extends Model
{
    public const MAX_COUNT = 6;

    protected $fillable = [
        'image_path',
        'caption',
        'sort_order',
        'is_active',
    ];

    protected function casts(): array
    {
        return [
            'sort_order' => 'integer',
            'is_active' => 'boolean',
        ];
    }

    public function imageUrl(): string
    {
        if ($this->image_path !== '' && Storage::disk('public')->exists($this->image_path)) {
            return Storage::disk('public')->url($this->image_path);
        }

        return '';
    }

    /**
     * @return Collection<int, static>
     */
    public static function forLanding(): Collection
    {
        return static::query()
            ->where('is_active', true)
            ->orderBy('sort_order')
            ->orderBy('id')
            ->get();
    }
}
