<?php

declare(strict_types=1);

namespace PpPhp\Core\Designer;

use PpPhp\Core\Platform\Metadata\ValueObjects\PlatformMetadata;

/**
 * Factory/value builder for Designer JSON components.
 *
 * Rule: every visual/control item must have a stable id/name so the JS
 * properties panel can target it exactly.
 */
class DesignerComponent
{
    public static function make(
        string $id,
        string $type,
        string $label,
        array $props = [],
        array $children = [],
        array $style = [],
        array $events = [],
        array $visibility = [],
        array $meta = [],
    ): array {
        $safeId = self::safeId($id);

        return [
            'id' => $safeId,
            'name' => $safeId,
            'type' => $type,
            'label' => $label,
            'props' => $props,
            'children' => $children,
            'style' => self::style($style),
            'events' => self::events($events),
            'visibility' => self::visibility($visibility),
            'meta' => $meta,
        ];
    }

    private ?PlatformMetadata $metadata = null;

    public function setMetadata(PlatformMetadata $metadata): self
    {
        $this->metadata = $metadata;

        return $this;
    }

    public function metadata(): ?PlatformMetadata
    {
        return $this->metadata;
    }





    public static function breadcrumb(
        string $id,
        array $items,
        string $style = 'simple',
        bool $showHome = true
    ): array {
        return self::make($id, 'breadcrumb', 'Breadcrumb', [
            'breadcrumbStyle' => $style,
            'showHome' => $showHome,
            'items' => $items,
        ]);
    }

    public static function heading(
        string $id,
        string $title,
        string $subtitle = '',
        string $tag = 'h2'
    ): array {
        return self::make($id, 'heading', 'Page Heading', [
            'text' => $title,
            'subtitle' => $subtitle,
            'tag' => $tag,
        ]);
    }

    public static function validationSummary(
        string $id = 'validation_summary',
        string $message = 'Please correct the highlighted errors.',
        bool $hidden = true
    ): array {
        return self::make($id, 'validation-summary', 'Validation Summary', [
            'hidden' => $hidden,
            'message' => $message,
        ]);
    }


    public static function section(
        string $id,
        string $title,
        array $children = [],
        array $style = []
    ): array {
        return self::make($id, 'section', $title, [
            'title' => $title,
        ], [], $style) + [
            'children' => $children,
        ];
    }


    public static function row(string $id, string $label, array $cells, array $style = []): array
    {
        return self::make($id, 'row', $label, [
            'columns' => count($cells),
            'cells' => $cells,
        ], [], $style);
    }

    public static function cell(string $id, int $span, array $components = [], array $style = []): array
    {
        $safeId = self::safeId($id);

        return [
            'id' => $safeId,
            'name' => $safeId,
            'span' => max(1, min(12, $span)),
            'components' => $components,
            'style' => self::style($style),
            'events' => self::events(),
            'visibility' => self::visibility(),
        ];
    }

    public static function field(
        string $fieldName,
        string $label,
        bool $required = false,
        bool $readonly = false,
        array $props = [],
        array $style = [],
        array $events = [],
        array $visibility = []
    ): array {
        $fieldId = self::safeId($fieldName) . '_field';

        return self::make($fieldId, 'field', $label, array_merge([
            'field' => $fieldName,
            'label' => $label,
            'required' => $required,
            'readonly' => $readonly,
            'placeholder' => '',
            'defaultValue' => '',
            'mode' => $readonly ? 'view' : 'edit',
        ], $props), [], $style, $events, $visibility);
    }

    public static function button(
        string $id,
        string $text,
        string $variant = 'primary',
        string $icon = '',
        string $eventContext = 'page',
        array $events = [],
        array $style = [],
        array $visibility = []
    ): array {
        return self::make($id, 'button', $text, [
            'text' => $text,
            'variant' => $variant,
            'icon' => $icon,
            'eventContext' => $eventContext,
        ], [], $style, self::events($events), $visibility);
    }

    public static function buttonGroup(
        string $id,
        array $buttons,
        string $label = 'Button Group',
        string $display = 'text'
    ): array {
        return self::make($id, 'button-group', $label, [
            'type' => 'button-group',
            'buttons' => $buttons,
            'display' => $display,
        ], $buttons);
    }

    public static function text(
        string $id,
        string $text,
        string $label = 'Text',
        array $style = []
    ): array {
        return self::make($id, 'text', $label, [
            'text' => $text,
        ], [], $style);
    }

    public static function style(array $overrides = []): array
    {
        return array_merge([
            'class' => '',
            'customCss' => '',
            'margin' => '',
            'padding' => '',
            'width' => '',
            'height' => '',
            'textAlign' => '',
            'fontSize' => '',
            'fontWeight' => '',
            'lineHeight' => '',
            'color' => '',
            'background' => '',
            'border' => '',
            'borderRadius' => '',
            'boxShadow' => '',
            'display' => '',
            'justifyContent' => '',
            'alignItems' => '',
        ], $overrides);
    }

    public static function events(array $overrides = []): array
    {
        return array_merge([
            'onClick' => '',
            'onChange' => '',
            'onLoad' => '',
            'server' => '',
            'context' => '',
        ], $overrides);
    }

    public static function visibility(array $overrides = []): array
    {
        return array_merge([
            'visible' => true,
            'roles' => [],
            'condition' => '',
            'device' => ['desktop', 'tablet', 'mobile'],
        ], $overrides);
    }

    public static function safeId(string $value): string
    {
        $value = strtolower(trim($value));
        $value = preg_replace('/[^a-z0-9_\-]+/', '_', $value) ?? $value;
        $value = trim($value, '_-');

        return $value !== '' ? $value : 'component';
    }
}
