<?php

namespace App\Models;

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

class StockModel extends Model
{
    use HasFactory, SoftDeletes;

    protected $table = 'barangs';
    
    protected $fillable = [
        'nama_barang',
        'kode_barang',
        'kategori',
        'deskripsi',
        'harga_sewa',
        'stok',
        'image',
        'status'
    ];

    protected $dates = ['deleted_at'];

    // Relasi dengan Paket
    public function pakets()
    {
        return $this->belongsToMany(Paket::class, 'paket_barang', 'barang_id', 'paket_id')
                    ->withPivot('jumlah')
                    ->withTimestamps();
    }

    // Scope untuk barang yang masih ada stoknya
    public function scopeInStock($query)
    {
        return $query->where('stok', '>', 0)
                    ->where('status', 'aktif');
    }

    // Accessor untuk mendapatkan URL gambar lengkap
    public function getImageUrlAttribute()
    {
        return $this->image ? asset('storage/' . $this->image) : null;
    }
}
