<?php

declare(strict_types=1);

namespace App\Models;

use Database\Factories\DocumentVersionFactory;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Support\Carbon;

/**
 * @property int $id
 * @property int $document_id
 * @property int $version
 * @property string $content
 * @property int|null $created_by
 * @property string|null $change_notes
 * @property Carbon|null $created_at
 * @property Carbon|null $updated_at
 * @property-read Document $document
 * @property-read User|null $creator
 *
 * @method static \Database\Factories\DocumentVersionFactory factory($count = null, $state = [])
 * @method static \Illuminate\Database\Eloquent\Builder<static>|DocumentVersion newModelQuery()
 * @method static \Illuminate\Database\Eloquent\Builder<static>|DocumentVersion newQuery()
 * @method static \Illuminate\Database\Eloquent\Builder<static>|DocumentVersion query()
 * @method static \Illuminate\Database\Eloquent\Builder<static>|DocumentVersion whereChangeNotes($value)
 * @method static \Illuminate\Database\Eloquent\Builder<static>|DocumentVersion whereContent($value)
 * @method static \Illuminate\Database\Eloquent\Builder<static>|DocumentVersion whereCreatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder<static>|DocumentVersion whereCreatedBy($value)
 * @method static \Illuminate\Database\Eloquent\Builder<static>|DocumentVersion whereDocumentId($value)
 * @method static \Illuminate\Database\Eloquent\Builder<static>|DocumentVersion whereId($value)
 * @method static \Illuminate\Database\Eloquent\Builder<static>|DocumentVersion whereUpdatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder<static>|DocumentVersion whereVersion($value)
 *
 * @mixin \Eloquent
 */
class DocumentVersion extends Model
{
    /** @use HasFactory<DocumentVersionFactory> */
    use HasFactory;

    protected $fillable = [
        'document_id',
        'version',
        'content',
        'created_by',
        'change_notes',
    ];

    /**
     * @return array<string, string>
     */
    protected function casts(): array
    {
        return [
            'version' => 'integer',
        ];
    }

    /**
     * Get the document this version belongs to.
     *
     * @return BelongsTo<Document, $this>
     */
    public function document(): BelongsTo
    {
        return $this->belongsTo(Document::class);
    }

    /**
     * Get the user who created this version.
     *
     * @return BelongsTo<User, $this>
     */
    public function creator(): BelongsTo
    {
        return $this->belongsTo(User::class, 'created_by');
    }
}
