<?php
// app/Models/Base/TenantModel.php

namespace App\Models\Base;


use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Auth;

abstract class TenantModel extends Model  // Fixed: extends Model, not itself
{
    protected static function booted()
    {
        parent::booted();

        // Automatically add hotel_id when creating
        static::creating(function ($model) {
            if (Auth::check() && Auth::user()->hotel_id && !$model->hotel_id) {
                $model->hotel_id = Auth::user()->hotel_id;
            }
        });

        // Automatically scope queries to current hotel for non-super admins
        if (Auth::check() && Auth::user()->role !== 'super_admin') {
            static::addGlobalScope('hotel', function ($builder) {
                $builder->where('hotel_id', Auth::user()->hotel_id);
            });
        }
    }

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

    public function scopeByHotel($query, $hotelId)
    {
        return $query->where('hotel_id', $hotelId);
    }
}