<?php

namespace FluxCms\Core\FieldTypes;

class WysiwygField extends BaseField
{
    protected array $toolbar = ['bold', 'italic', 'link', 'bulletList', 'orderedList'];

    protected int $minHeight = 200;

    protected bool $allowImages = true;

    protected bool $allowTables = false;

    protected bool $allowCode = true;

    protected string $editor = 'tiptap'; // tiptap, tinymce, quill

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

        return $this;
    }

    public function minHeight(int $minHeight): static
    {
        $this->minHeight = $minHeight;

        return $this;
    }

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

        return $this;
    }

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

        return $this;
    }

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

        return $this;
    }

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

        return $this;
    }

    public function basic(): static
    {
        $this->toolbar = ['bold', 'italic'];
        $this->allowImages = false;
        $this->allowTables = false;
        $this->allowCode = false;

        return $this;
    }

    public function full(): static
    {
        $this->toolbar = [
            'bold', 'italic', 'underline', 'strike',
            'heading1', 'heading2', 'heading3',
            'bulletList', 'orderedList',
            'link', 'image', 'table',
            'code', 'codeBlock',
            'quote', 'rule',
        ];
        $this->allowImages = true;
        $this->allowTables = true;
        $this->allowCode = true;

        return $this;
    }

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

    public function getToolbar(): array
    {
        return $this->toolbar;
    }

    public function getMinHeight(): int
    {
        return $this->minHeight;
    }

    public function getAllowImages(): bool
    {
        return $this->allowImages;
    }

    public function getAllowTables(): bool
    {
        return $this->allowTables;
    }

    public function getAllowCode(): bool
    {
        return $this->allowCode;
    }

    public function getEditor(): string
    {
        return $this->editor;
    }

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

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

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

    public function sanitizeValue(mixed $value): mixed
    {
        if (! is_string($value)) {
            return $value;
        }

        // Basic HTML sanitization
        $value = trim($value);

        // Remove dangerous tags
        $dangerousTags = ['script', 'style', 'iframe', 'object', 'embed', 'form', 'input'];
        foreach ($dangerousTags as $tag) {
            $value = preg_replace('/<'.$tag.'[^>]*>.*?<\/'.$tag.'>/is', '', $value);
            $value = preg_replace('/<'.$tag.'[^>]*\/?>/is', '', $value);
        }

        // Remove javascript: links
        $value = preg_replace('/javascript:/i', '', $value);

        // Remove on* attributes
        $value = preg_replace('/\s*on\w+\s*=\s*["\'][^"\']*["\']/i', '', $value);

        return $value;
    }

    public function transformForDisplay(mixed $value): mixed
    {
        if (! is_string($value)) {
            return $value;
        }

        // Convert relative URLs to absolute
        $value = preg_replace_callback('/src="(\/[^"]*)"/', function ($matches) {
            return 'src="'.url($matches[1]).'"';
        }, $value);

        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,
            'toolbar' => $this->toolbar,
            'minHeight' => $this->minHeight,
            'allowImages' => $this->allowImages,
            'allowTables' => $this->allowTables,
            'allowCode' => $this->allowCode,
            'editor' => $this->editor,
            'helpText' => $this->helpText,
            'attributes' => $this->attributes,
        ];
    }
}
