<?php

declare(strict_types=1);

namespace App\Framework\View\Components;

use App\Framework\View\Attributes\ComponentName;
use App\Framework\View\Contracts\StaticComponent;
use App\Framework\View\Dom\ElementNode;
use App\Framework\View\Dom\Node;
use App\Framework\View\Dom\TextNode;

#[ComponentName('badge')]
final readonly class Badge implements StaticComponent
{
    private string $text;
    private string $variant;
    private string $size;
    private bool $pill;

    public function __construct(
        string $content = '',
        array $attributes = []
    ) {
        // Content is the badge text
        $this->text = $content;

        // Extract attributes with defaults
        $this->variant = $attributes['variant'] ?? 'default';
        $this->size = $attributes['size'] ?? 'md';
        $this->pill = isset($attributes['pill'])
            ? filter_var($attributes['pill'], FILTER_VALIDATE_BOOLEAN)
            : false;
    }

    public function getRootNode(): Node
    {
        $span = new ElementNode('span');

        // Build CSS classes
        $classes = ['badge', "badge--{$this->variant}", "badge--{$this->size}"];
        if ($this->pill) {
            $classes[] = 'badge--pill';
        }

        $span->setAttribute('class', implode(' ', $classes));

        // Add text content
        $textNode = new TextNode($this->text);
        $span->appendChild($textNode);

        return $span;
    }

    // Factory methods for programmatic usage (not needed for template usage)
    public static function create(string $text): self
    {
        return new self($text);
    }

    public static function primary(string $text): self
    {
        return new self($text, ['variant' => 'primary']);
    }

    public static function success(string $text): self
    {
        return new self($text, ['variant' => 'success']);
    }

    public static function warning(string $text): self
    {
        return new self($text, ['variant' => 'warning']);
    }

    public static function danger(string $text): self
    {
        return new self($text, ['variant' => 'danger']);
    }

    public static function info(string $text): self
    {
        return new self($text, ['variant' => 'info']);
    }
}
