<?php

declare(strict_types=1);

namespace App\Framework\LiveComponents\FormBuilder;

/**
 * Condition für conditional field rendering
 *
 * Beispiel: Company-Name nur anzeigen wenn account_type === 'business'
 */
final readonly class FieldCondition
{
    public function __construct(
        public string $fieldName,
        public string $operator,
        public mixed $value
    ) {
    }

    public static function equals(string $fieldName, mixed $value): self
    {
        return new self($fieldName, '===', $value);
    }

    public static function notEquals(string $fieldName, mixed $value): self
    {
        return new self($fieldName, '!==', $value);
    }

    public static function in(string $fieldName, array $values): self
    {
        return new self($fieldName, 'in', $values);
    }

    /**
     * Check if condition matches form data
     */
    public function matches(array $formData): bool
    {
        $fieldValue = $formData[$this->fieldName] ?? null;

        return match ($this->operator) {
            '===' => $fieldValue === $this->value,
            '!==' => $fieldValue !== $this->value,
            'in' => in_array($fieldValue, $this->value, true),
            default => false
        };
    }
}
