<?php

namespace App\Models\Witness;

use App\Models\Document\Document;
use App\Models\User\User;
use App\Trait\BelongsToSolicitor;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;

/**
 * An immutable snapshot of a witness statement at the moment it was generated.
 *
 * The answer and exhibit snapshots are plain JSON copies — deliberately NOT
 * foreign keys to the live rows — so a historical version never changes when
 * the client data or extracted evidence is edited later. This preserves a
 * reliable audit trail of exactly what was produced and when.
 */
class WitnessStatementVersion extends Model
{
    use BelongsToSolicitor;

    protected $fillable = [
        'solicitor_id',
        'witness_statement_id',
        'version_number',
        'document_id',
        'answers_snapshot',
        'exhibits_snapshot',
        'options',
        'generated_by',
        'generated_at',
    ];

    protected $casts = [
        'answers_snapshot' => 'array',
        'exhibits_snapshot' => 'array',
        'options' => 'array',
        'generated_at' => 'datetime',
    ];

    public function witnessStatement(): BelongsTo
    {
        return $this->belongsTo(WitnessStatement::class);
    }

    public function document(): BelongsTo
    {
        return $this->belongsTo(Document::class);
    }

    public function generatedBy(): BelongsTo
    {
        return $this->belongsTo(User::class, 'generated_by');
    }
}
