<?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\HasMany;

class Table extends Model
{
    use HasFactory;

    protected $fillable = [
        'branch_id',
        'name',
        'section',
        'capacity',
        'status',
        'is_active',
    ];

    protected $casts = [
        'is_active' => 'boolean',
    ];

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

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

    public function currentOrder()
    {
        return $this->orders()
            ->whereIn('status', ['pending', 'confirmed', 'preparing', 'ready'])
            ->latest()
            ->first();
    }

    public function getStatusDisplayAttribute(): string
    {
        return match($this->status) {
            'available' => 'Available',
            'occupied' => 'Occupied',
            'reserved' => 'Reserved',
            'maintenance' => 'Maintenance',
            default => ucfirst($this->status),
        };
    }

    public function getStatusColorAttribute(): string
    {
        return match($this->status) {
            'available' => '#10B981',    // green
            'occupied' => '#EF4444',     // red
            'reserved' => '#F59E0B',     // amber
            'maintenance' => '#6B7280',  // gray
            default => '#6B7280',        // gray
        };
    }

    public function getSectionDisplayAttribute(): string
    {
        return match($this->section) {
            'dining' => 'Dining Area',
            'bar' => 'Bar Area',
            'outdoor' => 'Outdoor',
            'vip' => 'VIP Section',
            'private' => 'Private Room',
            default => ucfirst($this->section),
        };
    }

    public function isAvailable(): bool
    {
        return $this->status === 'available' && $this->is_active;
    }

    public function isOccupied(): bool
    {
        return $this->status === 'occupied';
    }

    public function canBeOccupied(): bool
    {
        return $this->isAvailable();
    }

    public function occupy(): void
    {
        $this->update(['status' => 'occupied']);
    }

    public function makeAvailable(): void
    {
        $this->update(['status' => 'available']);
    }

    public function reserve(): void
    {
        $this->update(['status' => 'reserved']);
    }

    public function scopeAvailable($query)
    {
        return $query->where('status', 'available')->where('is_active', true);
    }

    public function scopeOccupied($query)
    {
        return $query->where('status', 'occupied');
    }

    public function scopeBySection($query, string $section)
    {
        return $query->where('section', $section);
    }

    public function scopeByBranch($query, int $branchId)
    {
        return $query->where('branch_id', $branchId);
    }
}