<?php

namespace Modules\Bms\Entities;

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

class BmsHallAvailability extends Model
{
    protected $table = 'bms_hall_availability';

    protected $fillable = [
        'business_id', 'hall_id', 'date', 'slot', 'status',
        'booking_id', 'hold_token', 'hold_expires_at', 'created_by', 'notes',
    ];

    protected $casts = [
        'date'            => 'date',
        'hold_expires_at' => 'datetime',
    ];

    public function hall(): BelongsTo
    {
        return $this->belongsTo(BmsHall::class, 'hall_id');
    }

    public function booking(): BelongsTo
    {
        return $this->belongsTo(BmsBooking::class, 'booking_id');
    }

    /** A hold is stale once its expiry has passed and it is still a hold. */
    public function scopeExpiredHolds($query)
    {
        return $query->where('status', 'hold')
            ->whereNotNull('hold_expires_at')
            ->where('hold_expires_at', '<', now());
    }
}
