<?php

declare(strict_types=1);

namespace PpPhp\Core\DataOps\Formats;

use PpPhp\Core\DataOps\ExportFormat;

/** JSON array of objects — for APIs, scripts and re-import. */
final class JsonFormat implements ExportFormat
{
    public static function id(): string { return 'json'; }
    public static function label(): string { return 'JSON'; }
    public static function icon(): array { return ['bi-filetype-json', 'var(--pp-fmt-json,#f59e0b)']; }
    public static function extension(): string { return 'json'; }
    public static function contentType(): string { return 'application/json; charset=utf-8'; }

    public function write(array $rows, array $columns, array $meta): void
    {
        $out = [];
        foreach ($rows as $r) {
            $item = [];
            foreach (array_keys($columns) as $c) $item[$c] = $r[$c] ?? null;
            $out[] = $item;
        }
        echo json_encode($out, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
    }
}
