<?php

namespace App\Models\Permissions;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Spatie\Permission\Models\Permission as SpatiePermission;

class PermissionOverride extends Model
{
    use SoftDeletes;

    protected $table = 'permission_overrides';

    protected $guarded = ['id'];

    protected $casts = [
        'tenant_id' => 'int',
        'model_id' => 'int',
        'permission_id' => 'int',
        'effect' => 'int',       // 1=ALLOW, -1=DENY
        'effective_from' => 'datetime',
        'effective_to' => 'datetime',
    ];

    /**
     * 예외가 걸린 주체(User, Department 등)
     * model_type, model_id를 사용하는 morphTo
     */
    public function model()
    {
        // morphTo(relationshipName, type, id)
        return $this->morphTo('model', 'model_type', 'model_id');
    }

    /** 연결된 Spatie Permission */
    public function permission()
    {
        return $this->belongsTo(SpatiePermission::class, 'permission_id');
    }
}
