<?php

namespace FluxCms\Core\FieldTypes;

class SelectField extends BaseField
{
    protected array $options = [];

    protected bool $multiple = false;

    protected bool $searchable = false;

    protected ?string $emptyOption = null;

    public function options(array $options): static
    {
        $this->options = $options;

        return $this;
    }

    public function multiple(bool $multiple = true): static
    {
        $this->multiple = $multiple;

        return $this;
    }

    public function searchable(bool $searchable = true): static
    {
        $this->searchable = $searchable;

        return $this;
    }

    public function emptyOption(string $text): static
    {
        $this->emptyOption = $text;

        return $this;
    }

    public function getType(): string
    {
        return 'select';
    }

    public function getOptions(): array
    {
        return $this->options;
    }

    public function isMultiple(): bool
    {
        return $this->multiple;
    }

    public function isSearchable(): bool
    {
        return $this->searchable;
    }

    public function getEmptyOption(): ?string
    {
        return $this->emptyOption;
    }

    public function getValidationRules(): array
    {
        $rules = [];

        if ($this->required) {
            $rules[] = 'required';
        }

        if ($this->multiple) {
            $rules[] = 'array';
        } else {
            $rules[] = 'string';
        }

        if (! empty($this->options)) {
            $validValues = array_keys($this->options);
            if ($this->multiple) {
                $rules[] = 'array';
                // Each value must be in the valid options
                foreach ($validValues as $value) {
                    $rules[] = 'array';
                }
            } else {
                $rules[] = 'in:'.implode(',', $validValues);
            }
        }

        return array_merge($rules, $this->rules);
    }

    public function getValue(array $content, ?string $locale = null): mixed
    {
        $value = parent::getValue($content, $locale);

        // Ensure array for multiple selects
        if ($this->multiple && ! is_array($value)) {
            return $value ? [$value] : [];
        }

        // Ensure string for single selects
        if (! $this->multiple && is_array($value)) {
            return ! empty($value) ? (string) $value[0] : '';
        }

        return $value;
    }

    public function toArray(): array
    {
        return [
            'type' => $this->getType(),
            'key' => $this->key,
            'label' => $this->label,
            'translatable' => $this->translatable,
            'required' => $this->required,
            'default' => $this->default,
            'options' => $this->options,
            'multiple' => $this->multiple,
            'searchable' => $this->searchable,
            'emptyOption' => $this->emptyOption,
            'helpText' => $this->helpText,
            'attributes' => $this->attributes,
        ];
    }
}
