<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;

class Product extends Model
{
    protected $fillable = [
        'name',
        'sku',
        'description',
        'type',
        'cost',
        'cost_price',
        'price',
        'stock_quantity',
        'min_stock_level',
        'unit_of_measure',
        'is_active',
        'menu_item_id' // Add this field
    ];

    protected $casts = [
        'cost' => 'decimal:2',
        'price' => 'decimal:2',
        'cost_price' => 'decimal:2',
        'stock_quantity' => 'integer',
        'min_stock_level' => 'integer',
        'is_active' => 'boolean',
    ];

    // Relationship with menu item
    public function menuItem()
    {
        return $this->belongsTo(MenuItem::class, 'menu_item_id');
    }

    // Manufacturing relationships
    public function manufacturingOrders()
    {
        return $this->hasMany(ManufacturingOrder::class);
    }

    public function consumptions()
    {
        return $this->hasMany(ManufacturingConsumption::class);
    }

    public function outputs()
    {
        return $this->hasMany(ManufacturingOutput::class);
    }

    // Scope for menu items
    public function scopeMenuItems($query)
    {
        return $query->whereNotNull('menu_item_id');
    }

    public function scopeRawMaterials($query)
    {
        return $query->where('type', 'raw');
    }

    public function scopeFinishedGoods($query)
    {
        return $query->where('type', 'finished');
    }
}