<?php

namespace App\Models\Scopes;

use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Scope;
use Illuminate\Support\Facades\Auth;

class ScoalaThroughElevScope implements Scope
{
    public function apply(Builder $builder, Model $model): void
    {
        $user = Auth::user();

        if (!$user || !$user->scoala_id) {
            return;
        }

        if ($user->hasRole('admin')) {
            return;
        }

        $builder->whereHas('elev.clasa', fn (Builder $q) => $q->where('scoala_id', $user->scoala_id));
    }
}
