<?php

namespace FluxCms\Core\Models;

use Illuminate\Database\Eloquent\Model;
use Spatie\Translatable\HasTranslations;

class CmsLinkedinPost extends Model
{
    use HasTranslations;

    protected $table = 'flux_cms_linkedin_posts';

    protected $fillable = [
        'linkedin_id',
        'title',
        'excerpt',
        'content',
        'author',
        'date',
        'url',
        'image',
        'tags',
        'source',
        'is_published',
        'order',
    ];

    /** @var array<string> */
    public array $translatable = ['title', 'excerpt', 'content'];

    protected function casts(): array
    {
        return [
            'title' => 'array',
            'excerpt' => 'array',
            'content' => 'array',
            'tags' => 'array',
            'date' => 'date',
            'is_published' => 'boolean',
            'order' => 'integer',
        ];
    }

    public function scopePublished($query)
    {
        return $query->where('is_published', true);
    }

    public function scopeOrdered($query)
    {
        return $query->orderBy('order')->orderByDesc('date');
    }

    public function scopeManual($query)
    {
        return $query->where('source', 'manual');
    }

    public function scopeFromApi($query)
    {
        return $query->where('source', 'api');
    }
}
