<?php

declare(strict_types=1);

namespace App\Framework\LiveComponents\FormBuilder;

/**
 * Definition eines einzelnen Formularfelds
 */
final readonly class FormFieldDefinition
{
    public function __construct(
        public FieldType $type,
        public string $name,
        public string $label,
        public bool $required = false,
        public ?string $placeholder = null,
        public mixed $defaultValue = null,
        public ?array $options = null, // Für select/radio
        public ?FieldCondition $showIf = null, // Conditional rendering
        public int $debounceMs = 300
    ) {
    }

    public static function text(
        string $name,
        string $label,
        bool $required = false,
        ?string $placeholder = null,
        ?string $defaultValue = null
    ): self {
        return new self(
            type: FieldType::TEXT,
            name: $name,
            label: $label,
            required: $required,
            placeholder: $placeholder,
            defaultValue: $defaultValue
        );
    }

    public static function email(
        string $name,
        string $label,
        bool $required = true,
        ?string $placeholder = null
    ): self {
        return new self(
            type: FieldType::EMAIL,
            name: $name,
            label: $label,
            required: $required,
            placeholder: $placeholder
        );
    }

    public static function radio(
        string $name,
        string $label,
        array $options,
        bool $required = false,
        ?string $defaultValue = null
    ): self {
        return new self(
            type: FieldType::RADIO,
            name: $name,
            label: $label,
            required: $required,
            defaultValue: $defaultValue,
            options: $options
        );
    }

    public static function checkbox(
        string $name,
        string $label,
        bool $defaultChecked = false
    ): self {
        return new self(
            type: FieldType::CHECKBOX,
            name: $name,
            label: $label,
            defaultValue: $defaultChecked
        );
    }

    public static function select(
        string $name,
        string $label,
        array $options,
        bool $required = false,
        ?string $defaultValue = null
    ): self {
        return new self(
            type: FieldType::SELECT,
            name: $name,
            label: $label,
            required: $required,
            defaultValue: $defaultValue,
            options: $options
        );
    }

    /**
     * Conditional field - only shown if condition matches
     */
    public function showWhen(FieldCondition $condition): self
    {
        return new self(
            type: $this->type,
            name: $this->name,
            label: $this->label,
            required: $this->required,
            placeholder: $this->placeholder,
            defaultValue: $this->defaultValue,
            options: $this->options,
            showIf: $condition
        );
    }

    /**
     * Check if field should be shown based on form data
     */
    public function shouldShow(array $formData): bool
    {
        if ($this->showIf === null) {
            return true;
        }

        return $this->showIf->matches($formData);
    }
}
