<?php

namespace App\Models;

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

use Illuminate\Notifications\Notifiable;
use Laravel\Sanctum\HasApiTokens;

class Berita extends Model
{
    use HasApiTokens, Notifiable, HasFactory;

    protected $fillable = [
        'judul',
        'kategori',
        'ringkasan',
        'isi',
        'gambar',
        'penulis',
        'status',
    ];

    protected $appends = ['gambar_url'];

    public function user()
    {
        return $this->belongsTo(\App\Models\User::class);
    }

    /**
     * Get the image URL attribute
     */
    public function getGambarUrlAttribute()
    {
        if ($this->gambar) {
            return asset('storage/' . $this->gambar);
        }
        return null;
    }

    /**
     * Scope for published berita
     */
    public function scopePublished($query)
    {
        return $query->where('status', 'published');
    }

    /**
     * Scope for draft berita
     */
    public function scopeDraft($query)
    {
        return $query->where('status', 'draft');
    }
}
