<?php
declare(strict_types=1);

namespace PpPhp\Core\Designer\Layout;

use PpPhp\Core\Designer\Templates\MinimalListPageTemplate;

final class DesignerLayoutVerifier
{
    public function verify(): array
    {
        $runtime = new DesignerLayoutRuntime();
        $row = $runtime->createRow('layout_row_1', 'two');
        $split = $runtime->splitColumn($row, 0, [3, 3]);
        $merged = $runtime->mergeColumns($split, 0, 1);
        $resized = $runtime->resizeColumn($merged, 0, 4);
        $reordered = $runtime->reorderColumn($runtime->createRow('layout_row_2', 'three'), 0, 2);
        $page = $runtime->addRowToPage((new MinimalListPageTemplate())->make('customers'), 'layout_row_3', 'sidebar_right', 1);
        $rowObject = DesignerGridRow::fromArray(['rowId' => $resized['id'], 'columns' => $resized['props']['columns']]);

        $checks = [
            'createRow' => ($row['type'] ?? '') === 'row' && count($row['props']['columns'] ?? []) === 2,
            'splitColumn' => count($split['props']['columns'] ?? []) === 3,
            'mergeColumns' => count($merged['props']['columns'] ?? []) === 2,
            'resizeColumn' => ($resized['props']['columns'][0]['span'] ?? 0) === 4,
            'reorderColumn' => count($reordered['props']['columns'] ?? []) === 3,
            'addRowToPage' => count($page['components'] ?? []) === 4 && (($page['components'][1]['id'] ?? '') === 'layout_row_3'),
            'valid12Grid' => $rowObject->valid() && $rowObject->totalSpan() <= 12,
            'statistics' => ($runtime->stats()['operations'] ?? 0) >= 6,
        ];

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

        return ['ok' => !in_array(false, $checks, true), 'checks' => $checks, 'issues' => $issues, 'summary' => ['checks' => count($checks), 'passed' => count(array_filter($checks)), 'failed' => count($checks) - count(array_filter($checks)), 'operations' => $runtime->stats()['operations'], 'score' => in_array(false, $checks, true) ? 80 : 100], 'operations' => $runtime->operations()];
    }
}
