<?php

namespace App\Models\Concerns;

use App\Support\Tenancy;
use Illuminate\Database\Eloquent\Builder;

trait BelongsToHotel
{
    protected static function bootBelongsToHotel(): void
    {
        static::creating(function ($model) {
            if (empty($model->hotel_id) && Tenancy::check()) {
                $model->hotel_id = Tenancy::id();
            }
        });

        static::addGlobalScope('hotel', function (Builder $builder) {
            if (Tenancy::check()) {
                $builder->where($builder->getModel()->getTable() . '.hotel_id', Tenancy::id());
            }
        });
    }

    public function hotel()
    {
        return $this->belongsTo(\App\Models\Hotel::class);
    }
}
