<?php

namespace App\Models;

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

class Struktur extends Model
{
    use HasFactory;

    protected $fillable = [
        'admin_id',
        'gambar_struktur',
        'status'
    ];

    protected $casts = [
        'status' => 'boolean'
    ];

    public function admin()
    {
        return $this->belongsTo(Admin::class);
    }

    /**
     * Get gambar struktur URL
     */
    public function getGambarStrukturUrlAttribute()
    {
        if ($this->gambar_struktur) {
            return asset('storage/struktur/' . $this->gambar_struktur);
        }
        return asset('images/default-img.png');
    }

    /**
     * Scope untuk struktur aktif
     */
    public function scopeActive($query)
    {
        return $query->where('status', true);
    }

    /**
     * Scope untuk urutan
     */
    public function scopeOrdered($query)
    {
        return $query->orderBy('created_at', 'desc');
    }
}