<?php

declare(strict_types=1);

namespace App\Support\WorkspaceIsolation;

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

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

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

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