<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Str;

class Materi extends Model
{
    use HasFactory;

    protected $fillable = [
        'judul',
        'slug',
        'konten',
        'kategori',
        'urutan',
        'is_published',
        'foto',
    ];

    protected $casts = [
        'is_published' => 'boolean',
        'urutan'       => 'integer',
    ];

    protected static function boot()
    {
        parent::boot();
        static::creating(function ($materi) {
            if (empty($materi->slug)) {
                $materi->slug = Str::slug($materi->judul);
            }
        });
        static::updating(function ($materi) {
            if ($materi->isDirty('judul')) {
                $materi->slug = Str::slug($materi->judul);
            }
        });
    }
}
