<?php

namespace App\Models;

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

class Product extends Model
{
    use HasFactory;

    protected $table = 'products';
    protected $primaryKey = 'id';

    protected $fillable = [
        'brand_id', //! Buat ambil data nama brand
        'category_id', //! Buat ambil data nama kategori
        'nama_produk',
        'deskripsi',
        'image',
        'harga',
        'status',
        'stok',
        'createdAt',
        'updatedAt',
        'deletedAt'
    ];
    public $timestamps = false;

    // Event yang berjalan otomatis sebelum update
    protected static function boot()
    {
        parent::boot();

        static::updating(function ($product) {
            if ($product->stok == 0) {
                $product->status = 'habis';
            }
        });
    }

    // Relasi ke Brand
    public function brand()
    {
        return $this->belongsTo(Brand::class, 'brand_id');
    }

    // Relasi ke Category
    public function category()
    {
        return $this->belongsTo(Category::class, 'category_id');
    }

}
