<?php

namespace App\Scopes;

use Illuminate\Contracts\Auth\Authenticatable;
use Illuminate\Database\Eloquent\Builder;

/**
 * Limits which global content entries a user may see in the admin. Entries
 * flagged superadmin_only are reserved for MTC users; everyone else only sees
 * unrestricted content. Wired into the content-manager listing via the
 * pages.global_content_query_scope config hook so the package stays free of
 * any role knowledge.
 */
class GlobalContentVisibilityScope
{
    public function apply(Builder $query, ?Authenticatable $user): Builder
    {
        return $query->when(
            $user?->hasRole('mtc') !== true,
            fn (Builder $builder) => $builder->where('superadmin_only', false)
        );
    }
}
