<?php

namespace App\Models;

use App\Traits\FilterableTrait;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Spatie\Translatable\HasTranslations;

class News extends Model
{
    use HasFactory, FilterableTrait, HasTranslations;

    public array $translatable = ['title', 'description'];

    protected $table = 'news';

    protected $fillable = [
        'title',
        'description',
        'image',
        'active',
        'start_date',
        'end_date',
        'sort',
    ];

    protected array $filterable = ['active'];
    protected array $searchable = ['title', 'description'];
    protected array $sortable = ['id', 'sort', 'created_at'];

    protected function casts(): array
    {
        return [
            'active' => 'boolean',
            'start_date' => 'date',
            'end_date' => 'date',
            'sort' => 'integer',
        ];
    }

    public function banners()
    {
        return $this->morphMany(Banner::class, 'linkable');
    }

    public function scopeForIndex($query)
    {
        return $query->select([
            'id',
            'title',
            'description',
            'image',
            'active',
            'start_date',
            'end_date',
            'sort',
            'created_at',
        ]);
    }
}
