<?php

declare(strict_types=1);

namespace ImovelZapAi\Publishing\Application\Services;

use ImovelZapAi\Publishing\Domain\Feeds\VrSync\ListingDataFeed;
use ImovelZapAi\Publishing\Infrastructure\Persistence\Eloquent\TenantFeedGenerationLogModel;

final class TenantFeedGenerationLogger
{
    /**
     * @param  list<string>  $errors
     */
    public function record(
        string $tenantId,
        int $listingCount,
        int $durationMs,
        bool $success,
        ?string $contentHash = null,
        array $errors = [],
    ): TenantFeedGenerationLogModel {
        /** @var TenantFeedGenerationLogModel $log */
        $log = TenantFeedGenerationLogModel::query()->create([
            'tenant_id' => $tenantId,
            'version' => ListingDataFeed::VERSION,
            'listing_count' => $listingCount,
            'content_hash' => $contentHash,
            'duration_ms' => $durationMs,
            'success' => $success,
            'errors' => $errors === [] ? null : json_encode($errors, JSON_UNESCAPED_UNICODE),
            'created_at' => now(),
        ]);

        return $log;
    }
}
