<?php

namespace Daylight\Core\Modules\PageBuilder\Entities;

class Field
{
    public array $options = [];

    public array $selectOptions = [];

    public array $items = [];

    public int $amountOfFiles;

    public string $relatedType;

    public function __construct(
        public string $type,
        public string $name,
        public string $label,
    ) {
        //
    }

    public function required(): self
    {
        $this->options['required'] = true;

        return $this;
    }

    public function withOptions(array $options = []): self
    {
        $this->selectOptions = $options;

        return $this;
    }

    public function withItems(array $items = []): self
    {
        $this->items = $items;

        return $this;
    }

    public function setRelatedType(string $relatedType): self
    {
        $this->relatedType = $relatedType;

        return $this;
    }

    public function placeholder(string $placeholder): self
    {
        $this->options['placeholder'] = $placeholder;

        return $this;
    }

    public function singleFile(): self
    {
        $this->amountOfFiles = 1;

        return $this;
    }

    public static function make(string $type, string $name, string $label): self
    {
        return new self($type, $name, $label);
    }

    public static function text(string $name, string $label): self
    {
        return new self('text', $name, $label);
    }

    public static function select(string $name, string $label, array $options): self
    {
        return (new self('select', $name, $label))
            ->withOptions($options);
    }

    public static function button(string $name, string $label): self
    {
        return new self('button', $name, $label);
    }

    public static function repeater(string $name, string $label, array $fields): self
    {
        return (new self('repeater', $name, $label))
            ->withItems($fields);
    }

    public static function wysiwyg(string $name, string $label): self
    {
        return new self('wysiwyg', $name, $label);
    }

    public static function media(string $name, string $label): self
    {
        return new self('media', $name, $label);
    }

    public static function related(string $name, string $label, string $type): self
    {
        return (new self('related', $name, $label))
            ->setRelatedType($type);
    }

    public static function form(string $name, string $label = 'Form'): self
    {
        return new self('form', $name, $label);
    }

    public function toArray(): array
    {
        $array = [
            'type' => $this->type,
            'name' => $this->name,
            'label' => $this->label,
            'options' => $this->options,
            'selectOptions' => $this->selectOptions,
            'items' => [],
        ];

        if (isset($this->amountOfFiles)) {
            $array['amountOfFiles'] = $this->amountOfFiles;
        }

        if (isset($this->relatedType)) {
            $array['relatedType'] = $this->relatedType;
        }

        if (! empty($this->items)) {
            $array['items'] = collect($this->items)->map(function ($item) {
                return $item instanceof self ? $item->toArray() : $item;
            })->toArray();
        }

        return $array;
    }
}
