<?php

declare(strict_types=1);

namespace App\Support\WorkspaceIsolation;

use App\Models\ManagedEnvironment;
use Illuminate\Database\Eloquent\Builder;

final class TenantOwnedQueryScope
{
    public function apply(Builder $query, ?ManagedEnvironment $tenant, string $relationship = 'tenant'): Builder
    {
        if (! $tenant instanceof ManagedEnvironment) {
            return $this->empty($query);
        }

        return $query->whereBelongsTo($tenant, $relationship);
    }

    public function empty(Builder $query): Builder
    {
        return $query->whereRaw('1 = 0');
    }
}
