<?php

namespace App\Models\Documents;

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

/**
 * 템플릿별 외부 키 매핑 정의
 */
class DocumentTemplateLink extends Model
{
    protected $table = 'document_template_links';

    protected $fillable = [
        'template_id',
        'link_key',
        'label',
        'link_type',
        'source_table',
        'search_params',
        'display_fields',
        'is_required',
        'sort_order',
    ];

    protected $casts = [
        'search_params' => 'array',
        'display_fields' => 'array',
        'is_required' => 'boolean',
        'sort_order' => 'integer',
    ];

    public function template(): BelongsTo
    {
        return $this->belongsTo(DocumentTemplate::class, 'template_id');
    }

    public function linkValues(): HasMany
    {
        return $this->hasMany(DocumentTemplateLinkValue::class, 'link_id')
            ->orderBy('sort_order');
    }
}
