<?php

namespace Kylin\Catering\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Kylin\Catering\Enums\TableStatus;

/**
 * 餐桌
 */
class Table extends Model
{
    protected $table = 'catering_tables';
    protected $fillable = [
        'table_number',
        'area',
        'seats',
        'status',
        'min_charge',
        'qr_code_url',
        'sort_order',
    ];

    protected $casts = [
        'status' => TableStatus::class,
        'seats' => 'integer',
        'min_charge' => 'decimal:2',
        'sort_order' => 'integer',
    ];

    public function orders(): HasMany
    {
        return $this->hasMany(Order::class);
    }

    public function currentOrder()
    {
        return $this->hasOne(Order::class)->whereNotIn('status', ['completed', 'cancelled'])->latest();
    }

    public function isAvailable(): bool
    {
        return $this->status === TableStatus::Available;
    }
}
