<?php

namespace App\Models;

use Filament\Forms\Components\RichEditor\FileAttachmentProviders\SpatieMediaLibraryFileAttachmentProvider;
use Filament\Forms\Components\RichEditor\Models\Concerns\InteractsWithRichContent;
use Filament\Forms\Components\RichEditor\Models\Contracts\HasRichContent;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Str;
use Spatie\MediaLibrary\HasMedia;
use Spatie\MediaLibrary\InteractsWithMedia;
use Spatie\Tags\HasTags;

/* 
  Entry model with rich content and media library integration
  This is the main article / blog rich content model
*/
class Entry extends Model implements HasRichContent, HasMedia

{

    use InteractsWithMedia, InteractsWithRichContent, HasFactory, HasTags;

    protected $fillable = [
        'title',
        'slug',
        'description',
        'is_published',
        'is_featured',
        'published_at',
        'content',
    ];


    /**
     * Set up rich content configuration for media library integration
     */
    public function setUpRichContent(): void
    {
        $this->registerRichContent('content')
            ->fileAttachmentProvider(
                SpatieMediaLibraryFileAttachmentProvider::make()
                    ->collection('content-attachments')
                    ->preserveFilenames()
            );
    }

    protected static function boot()
    {
        parent::boot();

        static::creating(function ($entry) {
            if (empty($entry->slug)) {
                $entry->slug = Str::slug($entry->title);
            }
        });
    }
}
