<?php

declare(strict_types=1);

namespace App\Services\Reports\Formatters;

/**
 * JSON-формат: pretty-printed массив объектов (header → value).
 *
 * { "headers": [...], "rows": [{header1: value1, ...}, ...] }
 *
 * UTF-8 без escape (UNESCAPED_UNICODE) — кириллица в исходном виде.
 * UNESCAPED_SLASHES — для URLs.
 */
class JsonFormatter implements ReportFormatter
{
    public function format(array $headers, array $rows): string
    {
        $items = [];
        foreach ($rows as $row) {
            $item = [];
            foreach ($headers as $colIdx => $header) {
                $item[$header] = $row[$colIdx] ?? null;
            }
            $items[] = $item;
        }

        return json_encode([
            'headers' => $headers,
            'rows' => $items,
        ], JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
    }

    public function fileExtension(): string
    {
        return 'json';
    }
}
