<?php

declare(strict_types=1);

namespace ImovelZapAi\Importing\Application\Services;

use ImovelZapAi\Importing\Domain\Enums\ImportLogEvent;
use ImovelZapAi\Importing\Infrastructure\Persistence\Eloquent\ImportLogModel;

final class ImportLogWriter
{
    /**
     * @param  array<string, mixed>|null  $diffBefore
     * @param  array<string, mixed>|null  $diffAfter
     * @param  array<string, mixed>  $context
     */
    public function write(
        string $tenantId,
        string $importJobId,
        ImportLogEvent $event,
        ?string $propertyId = null,
        ?string $externalId = null,
        ?int $listingIndex = null,
        ?string $result = null,
        ?array $diffBefore = null,
        ?array $diffAfter = null,
        int $photosCount = 0,
        ?int $durationMs = null,
        ?string $message = null,
        array $context = [],
    ): void {
        ImportLogModel::query()->create([
            'tenant_id' => $tenantId,
            'import_job_id' => $importJobId,
            'property_id' => $propertyId,
            'external_id' => $externalId,
            'listing_index' => $listingIndex,
            'event' => $event,
            'result' => $result,
            'diff_before' => $diffBefore,
            'diff_after' => $diffAfter,
            'photos_count' => $photosCount,
            'duration_ms' => $durationMs,
            'message' => $message,
            'context' => $context,
            'created_at' => now(),
        ]);
    }
}
