<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\SoftDeletes;

class MasterMenu extends Model
{
    use HasFactory;
    use SoftDeletes;

    protected $table = 'master_menu';

    protected $fillable = [
        'id_stand',
        'nama',
        'type_menu',
        'harga_pokok',
        'harga_jual',
        'image',
        'is_active',
        'is_package',
    ];

    protected $casts = [
        'harga_pokok' => 'decimal:2',
        'harga_jual' => 'decimal:2',
        'is_active' => 'boolean',
        'is_package' => 'boolean',
    ];

    public function stand(): BelongsTo
    {
        return $this->belongsTo(MasterStand::class, 'id_stand');
    }

    public function orderDetails(): HasMany
    {
        return $this->hasMany(OrdersDetail::class, 'id_menu');
    }

    public function packageComponents(): BelongsToMany
    {
        return $this->belongsToMany(
            self::class,
            'master_menu_package_items',
            'package_menu_id',
            'component_menu_id'
        )
            ->withPivot('qty')
            ->withTimestamps()
            ->whereNull('master_menu.deleted_at');
    }

    public function packageParents(): BelongsToMany
    {
        return $this->belongsToMany(
            self::class,
            'master_menu_package_items',
            'component_menu_id',
            'package_menu_id'
        )
            ->withPivot('qty')
            ->withTimestamps()
            ->whereNull('master_menu.deleted_at');
    }
}
