<?php

namespace App\Models;

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

class Product extends Model
{
    use HasFactory;
    protected $fillable = [
        'name',
        'description',
        'type',
        'cost',
        'price',
        'stock_quantity',
        'min_stock_alert',
        'serial_number',
        'image_path',
        'suppliers_id'
    ];


    public function hasLowStock(): bool
    {
        return $this->stock_quantity <= $this->min_stock_alert;
    }
    
    public function supplier()
    {
        return $this->belongsTo(Supplier::class);
    }
    public function saleDetails()
    {
        return $this->hasMany(SaleDetail::class);
    }

    public function sales()
    {
        return $this->belongsToMany(Sale::class, 'sale_details');
    }
}
/**$user->sales: Te da la lista de todas las compras de ese usuario.

$product->saleDetails->count(): Te dice cuántas veces aparece ese producto en tickets.**/