<?php

namespace App\Scopes;

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

/**
 * Limits which data modules (property categories) a user may see in the admin.
 * Categories flagged superadmin_only are reserved for MTC users; everyone else
 * only sees unrestricted modules. Applied to the property-categories listing,
 * which feeds both the /properties dashboard and the sidebar navigation.
 */
class PropertyCategoryVisibilityScope
{
    public function apply(Builder $query, ?Authenticatable $user): Builder
    {
        return $query->when(
            $user?->hasRole('mtc') !== true,
            fn (Builder $builder) => $builder->where('superadmin_only', false)
        );
    }
}
