<?php

namespace App\Models;

use App\Scopes\AvailableMenuItemScope;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use App\Models\BaseModel;

/**
 * @property int|null $order_type_id
 * @property string|null $order_type
 */
class KotItem extends BaseModel
{
    use HasFactory;

    protected $guarded = ['id'];

    public function menuItem(): BelongsTo
    {
        return $this->belongsTo(MenuItem::class)->withoutGlobalScope(AvailableMenuItemScope::class);
    }

    public function menuItemVariation(): BelongsTo
    {
        return $this->belongsTo(MenuItemVariation::class);
    }

    public function modifierOptions(): BelongsToMany
    {
        return $this->belongsToMany(ModifierOption::class, 'kot_item_modifier_options', 'kot_item_id', 'modifier_option_id');
    }

    public function kot(): BelongsTo
    {
        return $this->belongsTo(Kot::class);
    }

    public function cancelReason(): BelongsTo
    {
        return $this->belongsTo(KotCancelReason::class, 'cancel_reason_id');
    }

    public function orderItem(): BelongsTo
    {
        return $this->belongsTo(OrderItem::class);
    }

    public function cancelledBy(): BelongsTo
    {
        return $this->belongsTo(User::class, 'cancelled_by')->withoutGlobalScope(\App\Scopes\BranchScope::class);
    }
    /** POS_QUICK_CHARGE:displayLineName */
    public function displayLineName(): string
    {
        // POS_QUICK_CHARGE:BEGIN KotItem::displayLineName
        if (class_exists(\Modules\PosQuickCharge\Support\OrderItemLineName::class)) {
            return \Modules\PosQuickCharge\Support\OrderItemLineName::resolveKotItem($this);
        }
        // POS_QUICK_CHARGE:END KotItem::displayLineName

        return $this->menuItem?->item_name ?? '';
    }
}
