<?php

namespace App\Models\System;

use Spatie\Permission\Models\Role as SpatieRole;

class Role extends SpatieRole
{
    protected $fillable = [
        'name',
        'guard_name',
        'company_id',
        'is_system',
    ];

    protected $casts = [
        'is_system' => 'boolean',
    ];

    /**
     * Get the company that owns the role.
     */
    public function company()
    {
        return $this->belongsTo(Company::class);
    }

    /**
     * Scope a query to only include roles for a specific company or system roles.
     */
    public function scopeForCompany($query, $company_id)
    {
        return $query->where(function($q) use ($company_id) {
            $q->where('company_id', $company_id)
              ->orWhereNull('company_id');
        });
    }
}
