<?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\Database\Eloquent\Relations\BelongsTo;
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 HasMedia, HasRichContent
{
    use HasFactory, HasTags, InteractsWithMedia, InteractsWithRichContent;

    protected $fillable = [
        'title',
        'type',
        'slug',
        'description',
        'is_published',
        'is_featured',
        'published_at',
        'content',
        'call_to_action_link',
        'call_to_action_text',
        'category_id',
        'priority',
    ];

    /**
     * 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);
            }
        });
    }

    public function category(): BelongsTo
    {
        return $this->belongsTo(Category::class);
    }
}
