<?php

namespace App\Models\Concerns;

use App\Models\ResourceShare;
use App\Models\User;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Relations\MorphMany;

trait HasOwnerScope
{
    public function scopeOwnedOrSharedWith(Builder $query, ?User $user): Builder
    {
        if ($user === null) {
            return $query->whereRaw('1 = 0');
        }

        return $query->where('user_id', $user->id);
    }

    public function shares(): MorphMany
    {
        return $this->morphMany(ResourceShare::class, 'shareable');
    }
}
