<?php

declare(strict_types=1);

namespace App\Models\Concerns;

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

trait BelongsToBranch
{
    public function scopeVisibleTo(Builder $query, User $user): Builder
    {
        $branchIds = app(BranchAccessService::class)->accessibleBranchIds($user);

        if ($branchIds === null) {
            return $query;
        }

        return $query->whereIn($this->getTable().'.branch_id', $branchIds);
    }
}
