<?php

declare(strict_types=1);

namespace App\Framework\View\Components;

use App\Framework\View\ValueObjects\HtmlAttributes;
use App\Framework\View\ValueObjects\HtmlElement;
use App\Framework\View\ValueObjects\HtmlTag;
use App\Framework\View\ValueObjects\StandardHtmlElement;
use App\Framework\View\ValueObjects\TagName;

final readonly class FormTextarea implements HtmlElement
{
    public HtmlTag $tag;

    public HtmlAttributes $attributes;

    public string $content;

    public function __construct(
        public string $name,
        public ?string $value = null,
        public ?string $label = null,
        public ?string $id = null,
        public ?string $placeholder = null,
        public int $rows = 4,
        public ?int $cols = null,
        public bool $required = false,
        public bool $disabled = false,
        public bool $readonly = false,
        public ?int $minLength = null,
        public ?int $maxLength = null,
        public ?string $errorMessage = null,
        public HtmlAttributes $additionalAttributes = new HtmlAttributes()
    ) {
        $this->tag = new HtmlTag(TagName::DIV);
        $this->attributes = HtmlAttributes::empty()->withClass('form-group');
        $this->content = $this->buildContent();
    }

    private function buildContent(): string
    {
        $textareaId = $this->id ?? "textarea-{$this->name}";
        $elements = [];

        // Label
        if ($this->label !== null) {
            $labelAttrs = HtmlAttributes::empty()
                ->withClass('form-label')
                ->with('for', $textareaId);
            if ($this->required) {
                $labelAttrs = $labelAttrs->withClass('form-label--required');
            }
            $elements[] = StandardHtmlElement::create(TagName::LABEL, $labelAttrs, $this->label);
        }

        // Textarea
        $textareaAttrs = HtmlAttributes::empty()
            ->withName($this->name)
            ->withId($textareaId)
            ->withClass('form-textarea')
            ->with('rows', (string) $this->rows);

        if ($this->cols !== null) {
            $textareaAttrs = $textareaAttrs->with('cols', (string) $this->cols);
        }
        if ($this->placeholder !== null) {
            $textareaAttrs = $textareaAttrs->with('placeholder', $this->placeholder);
        }
        if ($this->required) {
            $textareaAttrs = $textareaAttrs->withRequired();
        }
        if ($this->disabled) {
            $textareaAttrs = $textareaAttrs->withDisabled();
        }
        if ($this->readonly) {
            $textareaAttrs = $textareaAttrs->withReadonly();
        }
        if ($this->minLength !== null) {
            $textareaAttrs = $textareaAttrs->with('minlength', (string) $this->minLength);
        }
        if ($this->maxLength !== null) {
            $textareaAttrs = $textareaAttrs->with('maxlength', (string) $this->maxLength);
        }
        if ($this->errorMessage !== null) {
            $textareaAttrs = $textareaAttrs->withClass('form-textarea--error')
                ->with('aria-invalid', 'true')
                ->with('aria-describedby', "{$textareaId}-error");
        }

        foreach ($this->additionalAttributes->attributes as $name => $value) {
            $textareaAttrs = $textareaAttrs->with($name, $value);
        }

        $elements[] = StandardHtmlElement::create(TagName::TEXTAREA, $textareaAttrs, $this->value ?? '');

        // Error message
        if ($this->errorMessage !== null) {
            $elements[] = StandardHtmlElement::create(
                TagName::SPAN,
                HtmlAttributes::empty()
                    ->withClass('form-error')
                    ->withId("{$textareaId}-error")
                    ->with('role', 'alert'),
                $this->errorMessage
            );
        }

        return implode('', array_map('strval', $elements));
    }

    public static function create(
        string $name,
        ?string $label = null,
        ?string $value = null
    ): self {
        return new self(name: $name, label: $label, value: $value);
    }

    public function withRows(int $rows): self
    {
        return new self(
            name: $this->name,
            value: $this->value,
            label: $this->label,
            id: $this->id,
            placeholder: $this->placeholder,
            rows: $rows,
            cols: $this->cols,
            required: $this->required,
            disabled: $this->disabled,
            readonly: $this->readonly,
            minLength: $this->minLength,
            maxLength: $this->maxLength,
            errorMessage: $this->errorMessage,
            additionalAttributes: $this->additionalAttributes
        );
    }

    public function withRequired(bool $required = true): self
    {
        return new self(
            name: $this->name,
            value: $this->value,
            label: $this->label,
            id: $this->id,
            placeholder: $this->placeholder,
            rows: $this->rows,
            cols: $this->cols,
            required: $required,
            disabled: $this->disabled,
            readonly: $this->readonly,
            minLength: $this->minLength,
            maxLength: $this->maxLength,
            errorMessage: $this->errorMessage,
            additionalAttributes: $this->additionalAttributes
        );
    }

    public function withError(string $errorMessage): self
    {
        return new self(
            name: $this->name,
            value: $this->value,
            label: $this->label,
            id: $this->id,
            placeholder: $this->placeholder,
            rows: $this->rows,
            cols: $this->cols,
            required: $this->required,
            disabled: $this->disabled,
            readonly: $this->readonly,
            minLength: $this->minLength,
            maxLength: $this->maxLength,
            errorMessage: $errorMessage,
            additionalAttributes: $this->additionalAttributes
        );
    }

    public function __toString(): string
    {
        return (string) StandardHtmlElement::create(
            $this->tag->name,
            $this->attributes,
            $this->content
        );
    }
}
