<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Support\Str;

class News extends Model
{
    use SoftDeletes;

    protected $fillable = [
        'title','title_ar','slug','excerpt','content','cover_image',
        'category','tags','source','is_published','published_at','views',
    ];
    protected $casts = [
        'tags'         => 'array',
        'is_published' => 'boolean',
        'published_at' => 'datetime',
    ];

    protected static function boot()
    {
        parent::boot();
        static::creating(function ($news) {
            $news->slug = $news->slug ?? Str::slug($news->title);
        });
    }

    public function scopePublished($q)
    {
        return $q->where('is_published', true)
                 ->where('published_at', '<=', now());
    }

    public function scopeByCategory($q, string $category)
    {
        return $q->where('category', $category);
    }

    public function incrementViews(): void
    {
        $this->increment('views');
    }
}
