<?php

namespace App\Models\ESign;

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

class EsignFieldTemplate extends Model
{
    protected $table = 'esign_field_templates';

    protected $fillable = [
        'tenant_id',
        'name',
        'description',
        'category',
        'file_path',
        'file_name',
        'file_hash',
        'file_size',
        'signer_count',
        'variables',
        'is_active',
        'created_by',
    ];

    protected $casts = [
        'signer_count' => 'integer',
        'variables' => 'array',
        'is_active' => 'boolean',
    ];

    public function scopeForTenant($query, $tenantId)
    {
        return $query->where('tenant_id', $tenantId);
    }

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

    public function items(): HasMany
    {
        return $this->hasMany(EsignFieldTemplateItem::class, 'template_id')->orderBy('sort_order');
    }

    public function creator(): BelongsTo
    {
        return $this->belongsTo(\App\Models\User::class, 'created_by');
    }
}
