<?php

declare(strict_types=1);

namespace App\Framework\Design\ValueObjects;

/**
 * Value Object für CSS-Klassennamen
 */
final readonly class CssClassName
{
    public function __construct(
        public string $name
    ) {
    }

    public static function fromString(string $className): self
    {
        return new self(trim($className, '. '));
    }

    /**
     * Prüft ob die Klasse BEM-Konvention folgt
     */
    public function isBemBlock(): bool
    {
        return preg_match('/^[a-z][a-z0-9-]*$/', $this->name) &&
               ! str_contains($this->name, '__') &&
               ! str_contains($this->name, '--');
    }

    /**
     * Prüft ob die Klasse ein BEM-Element ist
     */
    public function isBemElement(): bool
    {
        return preg_match('/^[a-z][a-z0-9-]*__[a-z][a-z0-9-]*$/', $this->name);
    }

    /**
     * Prüft ob die Klasse ein BEM-Modifier ist
     */
    public function isBemModifier(): bool
    {
        return str_contains($this->name, '--');
    }

    /**
     * Extrahiert BEM-Block-Namen
     */
    public function getBemBlock(): ?string
    {
        if (preg_match('/^([a-z][a-z0-9-]*)/', $this->name, $matches)) {
            return $matches[1];
        }

        return null;
    }

    /**
     * Extrahiert BEM-Element-Namen
     */
    public function getBemElement(): ?string
    {
        if (preg_match('/__([a-z][a-z0-9-]*)/', $this->name, $matches)) {
            return $matches[1];
        }

        return null;
    }

    /**
     * Extrahiert BEM-Modifier-Namen
     */
    public function getBemModifier(): ?string
    {
        if (preg_match('/--([a-z][a-z0-9-]*)/', $this->name, $matches)) {
            return $matches[1];
        }

        return null;
    }

    /**
     * Prüft ob es eine Utility-Klasse ist (Tailwind-Style)
     */
    public function isUtilityClass(): bool
    {
        $utilityPatterns = [
            '/^[mp][trblxy]?-\d+$/',           // Margin/Padding
            '/^[wh]-\d+$/',                    // Width/Height
            '/^text-(xs|sm|base|lg|xl|\d+xl)$/', // Text sizes
            '/^(text|bg|border)-(red|blue|green|gray|yellow|purple|pink|indigo)-\d+$/', // Colors
            '/^(block|inline|flex|grid|hidden)$/', // Display
            '/^(justify|items|self)-(start|end|center|between|around)$/', // Flexbox
        ];

        foreach ($utilityPatterns as $pattern) {
            if (preg_match($pattern, $this->name)) {
                return true;
            }
        }

        return false;
    }

    /**
     * Erkennt Utility-Kategorie
     */
    public function getUtilityCategory(): ?string
    {
        $categoryPatterns = [
            'spacing' => '/^[mp][trblxy]?-\d+$/',
            'sizing' => '/^[wh]-\d+$/',
            'typography' => '/^text-(xs|sm|base|lg|xl|\d+xl|center|left|right)$/',
            'color' => '/^(text|bg|border)-(red|blue|green|gray|yellow|purple|pink|indigo)-\d+$/',
            'display' => '/^(block|inline|flex|grid|hidden)$/',
            'flexbox' => '/^(justify|items|self)-(start|end|center|between|around)$/',
        ];

        foreach ($categoryPatterns as $category => $pattern) {
            if (preg_match($pattern, $this->name)) {
                return $category;
            }
        }

        return null;
    }

    /**
     * Erkennt Component-Typ basierend auf Namen
     */
    public function getComponentType(): ?string
    {
        $componentPatterns = [
            'button' => ['btn', 'button'],
            'card' => ['card'],
            'modal' => ['modal', 'dialog'],
            'form' => ['form', 'input', 'select', 'textarea'],
            'navigation' => ['nav', 'navbar', 'menu'],
            'table' => ['table', 'thead', 'tbody', 'tr', 'td', 'th'],
            'alert' => ['alert', 'message', 'notification'],
            'badge' => ['badge', 'tag', 'chip'],
            'dropdown' => ['dropdown', 'select'],
            'tabs' => ['tab', 'tabs'],
            'accordion' => ['accordion', 'collapse'],
            'breadcrumb' => ['breadcrumb'],
            'pagination' => ['pagination', 'pager'],
        ];

        foreach ($componentPatterns as $componentType => $patterns) {
            foreach ($patterns as $pattern) {
                if (str_starts_with($this->name, $pattern) || str_contains($this->name, $pattern)) {
                    return $componentType;
                }
            }
        }

        return null;
    }

    /**
     * Analysiert Naming-Convention
     */
    public function getNamingConvention(): string
    {
        if ($this->isBemBlock() || $this->isBemElement() || $this->isBemModifier()) {
            return 'bem';
        }

        if (preg_match('/^[a-z][a-zA-Z0-9]*$/', $this->name)) {
            return 'camelCase';
        }

        if (preg_match('/^[a-z][a-z0-9-]*$/', $this->name)) {
            return 'kebab-case';
        }

        if (preg_match('/^[a-z][a-z0-9_]*$/', $this->name)) {
            return 'snake_case';
        }

        return 'other';
    }

    public function toString(): string
    {
        return $this->name;
    }
}
