<?php

namespace App\Models;

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

class Permission extends Model
{
    use HasFactory;

    protected $fillable = [
        'name',
        'display_name',
        'category',
        'description',
        'is_active',
    ];

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

    /**
     * RELATIONSHIPS
     */

    // Many-to-many with Roles
    public function roles()
    {
        return $this->belongsToMany(Role::class, 'role_permission')
            ->withTimestamps();
    }

    /**
     * SCOPES
     */

    // Active permissions
    public function scopeActive($query)
    {
        return $query->where('is_active', true);
    }

    // By category
    public function scopeByCategory($query, $category)
    {
        return $query->where('category', $category);
    }
}