<?php

namespace App\Models\Scopes;

use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Scope;

class TenantScope implements Scope
{
    // No-ops outside an authenticated session (queue workers, console
    // commands, public webhook endpoints)  those call sites have no
    // "current tenant" to compare against and must filter by an explicit
    // user_id of their own instead.
    public function apply(Builder $builder, Model $model): void
    {
        if (auth()->check()) {
            $builder->where($model->qualifyColumn('user_id'), auth()->id());
        }
    }
}
