<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Role extends Model
{
    use HasFactory;
    
    // Built In Roles
    public const ADMIN_ROLE = 1;
    public const TRIAL_ROLE = 2;
    public const BASIC_ROLE = 3;
    public const PRO_ROLE = 4;
    public const PREMIUM_ROLE = 5;
    public const CANCELLED_ROLE = 6;

    // Custom Roles
    // User
    public const OWNER_ROLE = 7;
    public const ADMINISTRATOR_ROLE = 8;
    public const BOOKKEEPER_ROLE = 9;
    public const COMPANY_SECRETARY_ROLE = 10;
    // Admin
    public const IT_PERSONNEL_ROLE = 11;
    public const NUMSTATION_MANAGER_ROLE = 12;
    public const NUMSTATION_STAFF_ROLE = 13;

    public function users()
    {
        return $this->hasMany(User::class);
    }
    
    public function rolePermissions()
    {
        return $this->hasMany(PermissionRole::class);
    }

    public function hasAccess($permissionKey)
    {
        $permission = Permission::where('key', $permissionKey)->first();
        if ($permission) {
            return $this->rolePermissions()->where('permission_id', $permission->id)->exists();
        }

        return false;
    }
}
