<?php

declare(strict_types=1);

namespace App\Framework\Design\Component;

final readonly class Component
{
    public function __construct(
        public string $name,
        public string $selector,
        public string $cssRules,
        public ComponentCategory $category,
        public ComponentPattern $pattern,
        public ComponentState $state,
        public string $filePath
    ) {
    }

    public function getId(): string
    {
        return md5($this->selector . $this->filePath);
    }

    public function getDisplayName(): string
    {
        return ucfirst(str_replace(['-', '_'], ' ', $this->name));
    }

    public function getPreviewHtml(): string
    {
        return match($this->category) {
            ComponentCategory::BUTTON => $this->generateButtonPreview(),
            ComponentCategory::NAVIGATION => $this->generateNavigationPreview(),
            ComponentCategory::FORM => $this->generateFormPreview(),
            ComponentCategory::CARD => $this->generateCardPreview(),
            ComponentCategory::FEEDBACK => $this->generateFeedbackPreview(),
            ComponentCategory::LAYOUT => $this->generateLayoutPreview(),
            ComponentCategory::TYPOGRAPHY => $this->generateTypographyPreview(),
            default => $this->generateDefaultPreview(),
        };
    }

    private function generateButtonPreview(): string
    {
        $text = match(true) {
            str_contains($this->name, 'primary') => 'Primary Button',
            str_contains($this->name, 'secondary') => 'Secondary Button',
            str_contains($this->name, 'success') => 'Success Button',
            str_contains($this->name, 'danger') => 'Danger Button',
            str_contains($this->name, 'warning') => 'Warning Button',
            default => 'Button',
        };

        return "<button class=\"{$this->name}\">{$text}</button>";
    }

    private function generateNavigationPreview(): string
    {
        if (str_contains($this->name, 'nav')) {
            return "<nav class=\"{$this->name}\"><a href=\"#\">Home</a><a href=\"#\">About</a><a href=\"#\">Contact</a></nav>";
        }

        return "<div class=\"{$this->name}\">Navigation Item</div>";
    }

    private function generateFormPreview(): string
    {
        if (str_contains($this->name, 'input')) {
            return "<input type=\"text\" class=\"{$this->name}\" placeholder=\"Enter text...\">";
        }

        if (str_contains($this->name, 'select')) {
            return "<select class=\"{$this->name}\"><option>Option 1</option><option>Option 2</option></select>";
        }

        return "<div class=\"{$this->name}\">Form Element</div>";
    }

    private function generateCardPreview(): string
    {
        return "<div class=\"{$this->name}\"><h3>Card Title</h3><p>Card content goes here...</p></div>";
    }

    private function generateFeedbackPreview(): string
    {
        $message = match(true) {
            str_contains($this->name, 'success') => 'Success! Operation completed successfully.',
            str_contains($this->name, 'error') || str_contains($this->name, 'danger') => 'Error! Something went wrong.',
            str_contains($this->name, 'warning') => 'Warning! Please check your input.',
            str_contains($this->name, 'info') => 'Info: Here is some information.',
            default => 'Alert message goes here.',
        };

        return "<div class=\"{$this->name}\">{$message}</div>";
    }

    private function generateLayoutPreview(): string
    {
        return "<div class=\"{$this->name}\"><div>Layout Item 1</div><div>Layout Item 2</div></div>";
    }

    private function generateTypographyPreview(): string
    {
        if (str_contains($this->name, 'heading') || str_contains($this->name, 'title')) {
            return "<h2 class=\"{$this->name}\">Heading Example</h2>";
        }

        return "<p class=\"{$this->name}\">Typography example text goes here.</p>";
    }

    private function generateDefaultPreview(): string
    {
        return "<div class=\"{$this->name}\">Component Preview</div>";
    }
}
