<?php
declare(strict_types=1);

namespace PpPhp\Core\Designer\Visual;

final class DesignerInspectorVisualRuntime
{
    public function inspectorFor(array $target): array
    {
        $type = (string) ($target['type'] ?? 'page');
        return [
            'targetId' => (string) ($target['id'] ?? 'page'),
            'targetType' => $type,
            'title' => $this->titleFor($type),
            'schemaKey' => $this->schemaKeyFor($type),
            'sections' => $this->sectionsFor($type),
            'mode' => 'schema-driven',
            'classes' => ['designer-inspector-v3', 'designer-inspector-' . $type],
        ];
    }

    public function supportedTargets(): array
    {
        return ['page','row','column','button','data_grid','grid_column','form','field','dashboard','dashboard_widget','chart','navigation_menu','auth_form'];
    }

    private function titleFor(string $type): string
    {
        return match ($type) {
            'row' => 'Row Properties',
            'column', 'grid_column' => 'Column Properties',
            'button' => 'Button Properties',
            'data_grid' => 'Grid Properties',
            'form' => 'Form Properties',
            'field' => 'Field Properties',
            'dashboard' => 'Dashboard Properties',
            'dashboard_widget' => 'Dashboard Widget Properties',
            'chart' => 'Chart Properties',
            'navigation_menu' => 'Navigation Properties',
            'auth_form' => 'Authentication Form Properties',
            default => 'Page Properties',
        };
    }

    private function schemaKeyFor(string $type): string
    {
        return match ($type) {
            'grid_column' => 'data_grid.column',
            'dashboard_widget' => 'dashboard.widget',
            default => str_replace('-', '_', $type),
        };
    }

    private function sectionsFor(string $type): array
    {
        return match ($type) {
            'data_grid', 'grid_column' => ['General','Columns','Data','Search & Filters','Actions','Appearance','Layout','Advanced'],
            'form', 'field', 'auth_form' => ['General','Fields','Validation','Data','Appearance','Layout','Events','Advanced'],
            'dashboard', 'dashboard_widget', 'chart' => ['General','Data','Appearance','Layout','Refresh','Permissions','Advanced'],
            'navigation_menu' => ['General','Menu Items','Routes','Permissions','Appearance','Layout','Advanced'],
            default => ['General','Appearance','Layout','Events','Advanced'],
        };
    }
}
