<?php

namespace App\Scopes;

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

/**
 * Automatically limits queries to the current tenant.
 * Scope is skipped when no tenant context is active (seeders, console, tests).
 */
class TenantScope implements Scope
{
    public function apply(Builder $builder, Model $model): void
    {
        if (!app()->bound('current_tenant_id')) {
            return;
        }

        $builder->where($model->getTable() . '.tenant_id', app('current_tenant_id'));
    }
}
