<?php

namespace Mtc\Modules\BlockRegistry\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;

class BlockTemplateItem extends Model
{
    protected $table = 'block_template_items';

    protected $fillable = [
        'block_template_id',
        'block_id',
        'source_block_id',
        'order_index',
    ];

    protected $casts = [
        'block_template_id' => 'integer',
        'block_id' => 'integer',
        'source_block_id' => 'integer',
        'order_index' => 'integer',
    ];

    public function blockTemplate(): BelongsTo
    {
        return $this->belongsTo(BlockTemplate::class, 'block_template_id');
    }

    public function block(): BelongsTo
    {
        return $this->belongsTo(BlockRegistry::class, 'block_id');
    }

    public function sourceBlock(): BelongsTo
    {
        return $this->belongsTo(EntityBlock::class, 'source_block_id');
    }

    public function isGlobalReference(): bool
    {
        return $this->source_block_id !== null;
    }
}
