<?php

namespace App\Models\Concerns;

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

trait BelongsToClient
{
    public function scopeOwnedBy(Builder $query, User|int|null $user): Builder
    {
        $userId = $user instanceof User ? $user->id : $user;

        return $query->where($this->getTable().'.user_id', (int) $userId);
    }

    public function ownedBy(User|int|null $user): bool
    {
        $userId = $user instanceof User ? $user->id : $user;

        return $userId !== null && (int) $this->user_id === (int) $userId;
    }
}
