<?php

declare(strict_types=1);

namespace App\Framework\View\Table\Generators;

use App\Framework\View\Table\Table;
use App\Framework\View\Table\TableColumn;
use App\Framework\View\Table\TableGenerator;
use App\Framework\View\Table\TableOptions;
use App\Framework\View\Table\TableRow;

final readonly class PhpInfoTableGenerator implements TableGenerator
{
    public function generate(object|array $data, ?TableOptions $options = null): Table
    {
        if (! is_array($data)) {
            throw new \InvalidArgumentException('PhpInfoTableGenerator expects array data');
        }

        $columns = [
            TableColumn::text('key', 'Setting', 'phpinfo-key'),
            TableColumn::text('value', 'Value', 'phpinfo-value'),
        ];

        $rows = [];
        foreach ($data as $item) {
            $rows[] = TableRow::fromData($item, $columns, 'phpinfo-row');
        }

        $tableOptions = $options ?? new TableOptions(
            striped: false,
            bordered: false,
            hover: true,
            responsive: true,
            emptyMessage: 'No PHP information available'
        );

        return new Table(
            columns: $columns,
            rows: $rows,
            cssClass: 'admin-table phpinfo-table',
            options: $tableOptions,
            id: 'phpInfoTable'
        );
    }

    public function supports(object|array $data): bool
    {
        if (! is_array($data) || empty($data)) {
            return false;
        }

        $firstItem = reset($data);
        if (! is_array($firstItem)) {
            return false;
        }

        // Check if it has key/value structure typical for PHP info
        return isset($firstItem['key']) && isset($firstItem['value']);
    }
}
