<?php
declare(strict_types=1);

namespace PpPhp\Core\Designer\Dashboard\Validation;

use PpPhp\Core\Designer\Dashboard\DesignerDashboardCard;
use PpPhp\Core\Designer\Dashboard\DesignerDashboardChart;
use PpPhp\Core\Designer\Dashboard\DesignerDashboardLayout;
use PpPhp\Core\Designer\Dashboard\DesignerDashboardQuickAction;
use PpPhp\Core\Designer\Dashboard\DesignerDashboardRuntime;

final class DesignerDashboardVerifier
{
    public function verify(): array
    {
        $runtime = new DesignerDashboardRuntime();

        $dashboard = $runtime->createDashboard('sales_dashboard', 'Sales Dashboard', '4+4+4');
        $dashboard = $runtime->addWidget($dashboard, DesignerDashboardCard::stat('customers', 'Customers', '1,245', 'bi-people', 3)->toArray());
        $dashboard = $runtime->addWidget($dashboard, DesignerDashboardCard::stat('orders', 'Orders', '890', 'bi-bag', 3)->toArray());
        $dashboard = $runtime->addWidget($dashboard, DesignerDashboardChart::chart('sales_chart', 'Sales', 'bar', 6)->toArray());
        $dashboard = $runtime->addWidget($dashboard, DesignerDashboardQuickAction::action('add_customer', 'Add Customer', 'customers_add', 'bi-plus', 3)->toArray());
        $dashboard = $runtime->duplicateWidget($dashboard, 'sales_chart');
        $dashboard = $runtime->moveWidget($dashboard, 'sales_chart_copy_1', 0);
        $dashboard = $runtime->deleteWidget($dashboard, 'orders');

        $admin = $runtime->template('admin');
        $crm = $runtime->template('crm');
        $helpdesk = $runtime->template('helpdesk');
        $page = $runtime->pageFor('admin');
        $html = $runtime->render($admin);
        $validation = $runtime->validate($admin);
        $stats = $runtime->stats();

        $layouts = [
            DesignerDashboardLayout::preset('4+4+4')->toArray(),
            DesignerDashboardLayout::preset('6+6')->toArray(),
            DesignerDashboardLayout::preset('3+3+3+3')->toArray(),
            DesignerDashboardLayout::preset('8+4')->toArray(),
            DesignerDashboardLayout::preset('9+3')->toArray(),
        ];

        $checks = [
            'dashboard' => ($dashboard['type'] ?? '') === 'dashboard',
            'widgets' => ($validation['widgets'] ?? 0) >= 6,
            'cards' => ($validation['cards'] ?? 0) >= 4,
            'charts' => ($validation['charts'] ?? 0) >= 1,
            'quickActions' => str_contains(json_encode($crm), 'quick_action'),
            'activityFeed' => str_contains(json_encode($helpdesk), 'activity_feed'),
            'layout' => count($layouts) === 5 && (($layouts[0]['type'] ?? '') === 'grid12'),
            'renderer' => str_contains($html, 'ppd-dashboard') && str_contains($html, 'data-widget-id'),
            'page' => ($page['pageType'] ?? '') === 'dashboard',
            'validation' => ($validation['ok'] ?? false) === true,
            'operations' => ($stats['operations'] ?? 0) >= 10,
        ];

        $issues = [];
        foreach ($checks as $check => $ok) {
            if (!$ok) {
                $issues[] = ['check' => $check, 'message' => 'Designer dashboard 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)),
                'widgets' => $validation['widgets'] ?? 0,
                'cards' => $validation['cards'] ?? 0,
                'charts' => $validation['charts'] ?? 0,
                'quickActions' => substr_count(json_encode([$admin, $crm, $helpdesk]), 'quick_action'),
                'layouts' => count($layouts),
                'score' => in_array(false, $checks, true) ? 80 : 100,
            ]),
        ];
    }
}
