<?php

declare(strict_types=1);

namespace ImovelZapAi\Documents\Application\Services;

use ImovelZapAi\Documents\Domain\Enums\DocumentHistoryEvent;
use ImovelZapAi\Documents\Infrastructure\Persistence\Eloquent\DocumentHistoryModel;

final class DocumentHistoryWriter
{
    /** @param  array<string, mixed>|null  $payload */
    public function write(
        string $tenantId,
        string $documentId,
        DocumentHistoryEvent $event,
        string $actorType = 'system',
        ?int $actorUserId = null,
        ?array $payload = null,
        ?string $message = null,
    ): void {
        DocumentHistoryModel::query()->create([
            'tenant_id' => $tenantId,
            'business_document_id' => $documentId,
            'event' => $event,
            'actor_type' => $actorType,
            'actor_user_id' => $actorUserId,
            'payload' => $payload,
            'message' => $message,
            'created_at' => now(),
        ]);
    }
}
