<?php

namespace App\Models\ESign;

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

class EsignContract extends Model
{
    use SoftDeletes;

    protected $table = 'esign_contracts';

    protected $fillable = [
        'tenant_id',
        'contract_code',
        'title',
        'description',
        'sign_order_type',
        'original_file_path',
        'original_file_name',
        'original_file_hash',
        'original_file_size',
        'signed_file_path',
        'signed_file_hash',
        'status',
        'send_method',
        'sms_fallback',
        'metadata',
        'expires_at',
        'completed_at',
        'created_by',
        'updated_by',
        'deleted_by',
    ];

    protected $casts = [
        'original_file_size' => 'integer',
        'sms_fallback' => 'boolean',
        'metadata' => 'array',
        'expires_at' => 'datetime',
        'completed_at' => 'datetime',
    ];

    public function signers(): HasMany
    {
        return $this->hasMany(EsignSigner::class, 'contract_id');
    }

    public function signFields(): HasMany
    {
        return $this->hasMany(EsignSignField::class, 'contract_id');
    }

    public function auditLogs(): HasMany
    {
        return $this->hasMany(EsignAuditLog::class, 'contract_id');
    }

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