<?php
declare(strict_types=1);

namespace PpPhp\Core\Designer\DataGrid\Validation;

use PpPhp\Core\Designer\DataGrid\DesignerDataGridRuntime;

final class DesignerDataGridVerifier
{
    public function verify(): array
    {
        $runtime = new DesignerDataGridRuntime();

        $grid = $runtime->fromFields('customers_grid', [
            ['name' => 'id', 'type' => 'number', 'label' => 'ID'],
            ['name' => 'first_name', 'type' => 'text', 'label' => 'First Name'],
            ['name' => 'email', 'type' => 'email', 'label' => 'Email'],
            ['name' => 'status', 'type' => 'badge', 'label' => 'Status'],
            ['name' => 'created_at', 'type' => 'datetime', 'label' => 'Created'],
        ], 'Customers', 'customers');

        $grid = $runtime->addColumn($grid, ['name' => 'avatar', 'type' => 'avatar', 'label' => 'Avatar']);
        $grid = $runtime->addFilter($grid, ['field' => 'status', 'type' => 'select', 'label' => 'Status']);
        $page = $runtime->pageFor('customers', [
            ['name' => 'id', 'type' => 'number'],
            ['name' => 'name', 'type' => 'text'],
            ['name' => 'email', 'type' => 'email'],
        ]);
        $html = $runtime->render($grid);
        $validation = $runtime->validate($grid);
        $stats = $runtime->stats();

        $props = $grid['props'] ?? [];

        $checks = [
            'columns' => ($validation['columnCount'] ?? 0) === 6,
            'rowActions' => ($validation['rowActions'] ?? 0) >= 4,
            'bulkActions' => ($validation['bulkActions'] ?? 0) >= 3,
            'filters' => ($validation['filterCount'] ?? 0) >= 6,
            'toolbar' => count((array) ($props['toolbar']['items'] ?? [])) >= 5,
            'pagination' => count((array) ($props['pagination']['pageSizes'] ?? [])) >= 4,
            'renderer' => str_contains($html, 'ppd-data-grid') && str_contains($html, 'data-row-action'),
            'pageFor' => ($page['pageType'] ?? '') === 'list' && count($page['components'] ?? []) === 2,
            'validation' => ($validation['ok'] ?? false) === true,
            'operations' => ($stats['operations'] ?? 0) >= 4,
        ];

        $issues = [];
        foreach ($checks as $check => $ok) {
            if (!$ok) {
                $issues[] = ['check' => $check, 'message' => 'Designer data grid verification failed: ' . $check];
            }
        }

        return [
            'ok' => !in_array(false, $checks, true),
            'checks' => $checks,
            'issues' => $issues,
            'summary' => array_replace($stats, [
                'checks' => count($checks),
                'passed' => count(array_filter($checks)),
                'failed' => count($checks) - count(array_filter($checks)),
                'columns' => $validation['columnCount'] ?? 0,
                'rowActions' => $validation['rowActions'] ?? 0,
                'bulkActions' => $validation['bulkActions'] ?? 0,
                'filters' => $validation['filterCount'] ?? 0,
                'toolbarItems' => count((array) ($props['toolbar']['items'] ?? [])),
                'paginationSizes' => count((array) ($props['pagination']['pageSizes'] ?? [])),
                'score' => in_array(false, $checks, true) ? 80 : 100,
            ]),
        ];
    }
}
