<?php
declare(strict_types=1);

namespace PpPhp\Core\Designer\DataGrid;

final class DesignerDataGridRenderer
{
    public function render(array $gridComponent): string
    {
        $props = (array) ($gridComponent['props'] ?? $gridComponent);
        $columns = (array) ($props['columns'] ?? []);
        $toolbar = (array) ($props['toolbar'] ?? []);
        $filters = (array) ($props['filters'] ?? []);
        $rowActions = (array) ($props['rowActions'] ?? []);
        $bulkActions = (array) ($props['bulkActions'] ?? []);
        $pagination = (array) ($props['pagination'] ?? []);

        $html = [];
        $html[] = '<section class="ppd-data-grid" data-grid-id="' . $this->e((string) ($props['gridId'] ?? $props['id'] ?? 'grid')) . '">';
        $html[] = '<header class="ppd-grid-toolbar">';
        $html[] = '<h3>' . $this->e((string) ($props['title'] ?? 'Data Grid')) . '</h3>';

        if (($toolbar['showSearch'] ?? true) === true) {
            $html[] = '<input class="ppd-grid-search" type="search" placeholder="Search...">';
        }

        foreach ((array) ($toolbar['items'] ?? []) as $item) {
            $html[] = '<button type="button" class="ppd-grid-toolbar-btn" data-action="' . $this->e((string) ($item['id'] ?? 'action')) . '">' . $this->e((string) ($item['label'] ?? 'Action')) . '</button>';
        }

        $html[] = '</header>';

        if ($filters !== []) {
            $html[] = '<div class="ppd-grid-filters">';
            foreach ($filters as $filter) {
                $html[] = '<label><span>' . $this->e((string) ($filter['label'] ?? $filter['field'] ?? 'Filter')) . '</span><input type="text" data-filter="' . $this->e((string) ($filter['field'] ?? '')) . '"></label>';
            }
            $html[] = '</div>';
        }

        if ($bulkActions !== []) {
            $html[] = '<div class="ppd-grid-bulk-actions">';
            foreach ($bulkActions as $action) {
                $html[] = '<button type="button" data-bulk-action="' . $this->e((string) ($action['id'] ?? 'bulk')) . '">' . $this->e((string) ($action['label'] ?? 'Bulk Action')) . '</button>';
            }
            $html[] = '</div>';
        }

        $html[] = '<table class="ppd-grid-table"><thead><tr>';
        $html[] = '<th class="ppd-grid-select"><input type="checkbox" data-grid-select-all></th>';
        foreach ($columns as $column) {
            if (($column['visible'] ?? true) === false) {
                continue;
            }
            $html[] = '<th data-column="' . $this->e((string) ($column['name'] ?? '')) . '">' . $this->e((string) ($column['label'] ?? $column['name'] ?? 'Column')) . '</th>';
        }
        if ($rowActions !== []) {
            $html[] = '<th class="ppd-grid-actions">Actions</th>';
        }
        $html[] = '</tr></thead><tbody><tr>';
        $html[] = '<td><input type="checkbox" data-grid-row-select></td>';
        foreach ($columns as $column) {
            if (($column['visible'] ?? true) === false) {
                continue;
            }
            $html[] = '<td data-cell="' . $this->e((string) ($column['name'] ?? '')) . '">{{ ' . $this->e((string) ($column['name'] ?? 'field')) . ' }}</td>';
        }
        if ($rowActions !== []) {
            $html[] = '<td class="ppd-grid-actions">';
            foreach ($rowActions as $action) {
                $html[] = '<button type="button" data-row-action="' . $this->e((string) ($action['id'] ?? 'action')) . '">' . $this->e((string) ($action['label'] ?? 'Action')) . '</button>';
            }
            $html[] = '</td>';
        }
        $html[] = '</tr></tbody></table>';

        $html[] = '<footer class="ppd-grid-pagination">';
        $html[] = '<span>Page size</span><select>';
        foreach ((array) ($pagination['pageSizes'] ?? [10, 25, 50, 100]) as $size) {
            $selected = ((int) $size === (int) ($pagination['pageSize'] ?? 25)) ? ' selected' : '';
            $html[] = '<option' . $selected . '>' . $this->e((string) $size) . '</option>';
        }
        $html[] = '</select>';
        if (($pagination['showTotals'] ?? true) === true) {
            $html[] = '<span class="ppd-grid-total">Showing 0 records</span>';
        }
        $html[] = '</footer>';

        $html[] = '</section>';

        return implode('', $html);
    }

    private function e(string $value): string
    {
        return htmlspecialchars($value, ENT_QUOTES, 'UTF-8');
    }
}
