<?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 FormCheckbox implements HtmlElement
{
    public HtmlTag $tag;

    public HtmlAttributes $attributes;

    public string $content;

    public function __construct(
        public string $name,
        public string $value = '1',
        public string $label = '',
        public bool $checked = false,
        public ?string $id = null,
        public bool $required = false,
        public bool $disabled = false,
        public ?string $errorMessage = null,
        public HtmlAttributes $additionalAttributes = new HtmlAttributes()
    ) {
        $this->tag = new HtmlTag(TagName::DIV);
        $this->attributes = HtmlAttributes::empty()->withClass('form-checkbox-group');
        $this->content = $this->buildContent();
    }

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

    public function withChecked(bool $checked = true): self
    {
        return new self(
            name: $this->name,
            value: $this->value,
            label: $this->label,
            checked: $checked,
            id: $this->id,
            required: $this->required,
            disabled: $this->disabled,
            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,
            checked: $this->checked,
            id: $this->id,
            required: $required,
            disabled: $this->disabled,
            errorMessage: $this->errorMessage,
            additionalAttributes: $this->additionalAttributes
        );
    }

    public function withError(string $errorMessage): self
    {
        return new self(
            name: $this->name,
            value: $this->value,
            label: $this->label,
            checked: $this->checked,
            id: $this->id,
            required: $this->required,
            disabled: $this->disabled,
            errorMessage: $errorMessage,
            additionalAttributes: $this->additionalAttributes
        );
    }

    private function buildContent(): string
    {
        $checkboxId = $this->id ?? "checkbox-{$this->name}";

        $attributes = HtmlAttributes::empty()
            ->withType('checkbox')
            ->withName($this->name)
            ->withValue($this->value)
            ->withId($checkboxId)
            ->withClass('form-checkbox');

        if ($this->checked) {
            $attributes = $attributes->withChecked();
        }

        if ($this->required) {
            $attributes = $attributes->withRequired();
        }

        if ($this->disabled) {
            $attributes = $attributes->withDisabled();
        }

        if ($this->errorMessage !== null) {
            $attributes = $attributes
                ->withClass('form-checkbox--error')
                ->with('aria-invalid', 'true')
                ->with('aria-describedby', "{$checkboxId}-error");
        }

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

        $elements = [];

        // Checkbox input
        $elements[] = StandardHtmlElement::create(TagName::INPUT, $attributes);

        // Label
        $labelAttrs = HtmlAttributes::empty()
            ->withClass('form-checkbox-label')
            ->with('for', $checkboxId);

        $elements[] = StandardHtmlElement::create(
            TagName::LABEL,
            $labelAttrs,
            $this->label
        );

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

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

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