<?php

declare(strict_types=1);

namespace PpPhp\Core\Designer\Registries;

use PpPhp\Core\Designer\Definitions\ComponentDefinition;

/**
 * ComponentRegistry
 *
 * Central catalogue of Designer components.
 *
 * This class intentionally keeps the current Designer array-component model intact.
 * It only standardizes component discovery for the left Components panel and D.makeComponent().
 */
final class ComponentRegistry
{
    /** @var array<string, ComponentDefinition> */
    private array $definitions = [];

    public static function default(): self
    {
        $registry = new self();
        $registry->registerDefaults();
        return $registry;
    }

    public function register(ComponentDefinition $definition): void
    {
        $this->definitions[$definition->type()] = $definition;
    }

    public function has(string $type): bool
    {
        return isset($this->definitions[$type]);
    }

    public function get(string $type): ?ComponentDefinition
    {
        return $this->definitions[$type] ?? null;
    }

    /** @return array<string, ComponentDefinition> */
    public function all(): array
    {
        return $this->definitions;
    }

    /** @return array<string, ComponentDefinition[]> */
    public function groups(): array
    {
        $groups = [];
        foreach ($this->definitions as $definition) {
            $groups[$definition->category()][] = $definition;
        }
        return $groups;
    }

    public function toArray(): array
    {
        return array_map(static fn(ComponentDefinition $d): array => $d->toArray(), $this->definitions);
    }

    private function def(string $type, string $label, string $category, string $icon = 'grip-vertical', string $description = '', array $defaults = [], array $classes = []): void
    {
        $this->register(new ComponentDefinition($type, $label, $category, $icon, $description, $defaults, $classes));
    }

    private function registerDefaults(): void
    {
        // Layouts
        $this->def('single_column', 'Single Column', 'Layouts', 'layout-text-window', 'One full-width layout row.', [], ['designer-layout-item']);
        $this->def('two_column', 'Two Columns', 'Layouts', 'layout-split', 'Two equal-width columns.', [], ['designer-layout-item']);
        $this->def('kpi_grid', 'KPI Grid', 'Layouts', 'grid-3x3-gap', 'Four-cell KPI layout.', [], ['designer-layout-item']);
        $this->def('analytics_grid', 'Analytics Grid', 'Layouts', 'bar-chart-line', 'Two chart areas and insight areas.', [], ['designer-layout-item']);

        // Structure
        $this->def('section', 'Section', 'Structure', 'collection', 'Container section.');
        $this->def('row', 'Row', 'Structure', 'layout-three-columns', '12-column layout row.', ['columns' => 2, 'cells' => [['span' => 6, 'components' => [], 'style' => []], ['span' => 6, 'components' => [], 'style' => []]]]);
        $this->def('column', 'Column', 'Structure', 'layout-sidebar', 'Free column area.');
        $this->def('tabs', 'Tabs', 'Structure', 'segmented-nav');
        $this->def('accordion', 'Accordion', 'Structure', 'list-nested');
        $this->def('modal', 'Modal', 'Structure', 'window');
        $this->def('card', 'Card', 'Structure', 'card-text');
        $this->def('group-box', 'Group Container', 'Structure', 'bounding-box');

        // Data and fields
        $this->def('table-grid', 'Table Grid', 'Data', 'table', 'Data grid with actions, search and bulk actions.', ['selection' => true, 'actions' => ['view','edit','delete'], 'bulkActions' => ['delete_selected','update_selected'], 'actionDisplay' => 'icon_text']);
        $this->def('field', 'Field', 'Data', 'input-cursor-text');
        $this->def('field-selector', 'Field Selector', 'Data', 'check2-square');
        $this->def('search', 'Search', 'Data', 'search');
        $this->def('advanced-search', 'Advanced Search', 'Data', 'search-heart');
        $this->def('filter-panel', 'Filter Panel', 'Data', 'funnel');
        $this->def('pagination', 'Pagination', 'Data', '123');
        $this->def('page-size', 'Page Size', 'Data', 'list-ol');
        $this->def('totals', 'Totals', 'Data', 'calculator');
        $this->def('master-detail', 'Master Detail', 'Data', 'diagram-3');

        // Actions and navigation
        $this->def('toolbar', 'Toolbar', 'Actions', 'tools');
        $this->def('buttons', 'Buttons', 'Actions', 'menu-button-wide');
        $this->def('custom-button', 'Custom Button', 'Actions', 'play-btn');
        $this->def('button', 'Button', 'Actions', 'cursor');
        $this->def('button-group', 'Button Group', 'Actions', 'collection');
        $this->def('breadcrumb', 'Breadcrumb', 'Navigation', 'signpost-split');
        $this->def('download-link', 'Download Link', 'Navigation', 'download');
        $this->def('external-link', 'External Link', 'Navigation', 'box-arrow-up-right');
        $this->def('offcanvas', 'Offcanvas', 'Navigation', 'layout-sidebar-inset');

        // Content
        $this->def('text', 'Text', 'Content', 'fonts');
        $this->def('html', 'HTML', 'Content', 'code-slash');
        $this->def('php-snippet', 'PHP Snippet', 'Content', 'filetype-php');
        $this->def('js-snippet', 'JS Snippet', 'Content', 'filetype-js');
        $this->def('css-snippet', 'CSS Snippet', 'Content', 'filetype-css');
        $this->def('code-snippet', 'Code Snippet', 'Content', 'code');
        $this->def('image', 'Image', 'Content', 'image');
        $this->def('icon', 'Icon', 'Content', 'star');
        $this->def('iframe', 'Iframe', 'Content', 'window');
        $this->def('video', 'Video', 'Content', 'play-circle');
        $this->def('upload', 'Upload', 'Content', 'upload');
        $this->def('print-panel', 'Print Panel', 'Content', 'printer');

        // Feedback and UI
        $this->def('status-badge', 'Status Badge', 'Feedback', 'patch-check');
        $this->def('alert', 'Alert', 'Feedback', 'exclamation-triangle');
        $this->def('toast', 'Toast', 'Feedback', 'bell');
        $this->def('progress', 'Progress', 'Feedback', 'bar-chart-steps');
        $this->def('rating', 'Rating', 'Feedback', 'star-half');
        $this->def('avatar', 'Avatar', 'Feedback', 'person-circle');
        $this->def('qr-code', 'QR Code', 'Feedback', 'qr-code');
        $this->def('signature', 'Signature', 'Feedback', 'pen');
        $this->def('wizard-step', 'Wizard Step', 'Feedback', '123');

        // Dashboard widgets
        $this->def('stat-card', 'Stat Card', 'Dashboard Widgets', 'bar-chart', 'Basic statistic card.', [], ['designer-dashboard-widget']);
        $this->def('kpi-trend-card', 'KPI Trend Card', 'Dashboard Widgets', 'graph-up-arrow', 'KPI metric with trend indicator.', [], ['designer-dashboard-widget']);
        $this->def('chart', 'Chart', 'Dashboard Widgets', 'graph-up', 'Chart placeholder/widget.', [], ['designer-dashboard-widget']);
        $this->def('recent-activity', 'Recent Activity', 'Dashboard Widgets', 'clock-history', 'Activity feed widget.', [], ['designer-dashboard-widget']);
        $this->def('notification-list', 'Notifications', 'Dashboard Widgets', 'bell', 'Notification list widget.', [], ['designer-dashboard-widget']);
        $this->def('quick-links', 'Quick Links', 'Dashboard Widgets', 'link-45deg', 'Quick action links.', [], ['designer-dashboard-widget']);
        $this->def('task-list', 'Task List', 'Dashboard Widgets', 'check2-square', 'Task list widget.', [], ['designer-dashboard-widget']);
        $this->def('progress-card', 'Progress Card', 'Dashboard Widgets', 'speedometer2', 'Progress metric card.', [], ['designer-dashboard-widget']);
        $this->def('mini-table', 'Mini Table', 'Dashboard Widgets', 'table', 'Small dashboard table.', [], ['designer-dashboard-widget']);
        $this->def('calendar-widget', 'Calendar Widget', 'Dashboard Widgets', 'calendar3', 'Dashboard calendar widget.', [], ['designer-dashboard-widget']);

        // Advanced blocks
        $this->def('custom-map', 'Custom Map', 'Advanced', 'geo-alt');
        $this->def('timeline', 'Timeline', 'Advanced', 'clock');
        $this->def('kanban', 'Kanban', 'Advanced', 'kanban');
        $this->def('calendar', 'Calendar', 'Advanced', 'calendar3');
    }
}
