<?php

declare(strict_types=1);

final class MyronPublicRenderPresentation
{
    /**
     * @param list<string> $extraClasses
     */
    public function buttonClasses(string $intent, string $appearance, string $size, array $extraClasses = []): string
    {
        $intent = $this->allow($intent, ['primary', 'neutral', 'danger'], 'primary');
        $appearance = $this->allow($appearance, ['solid', 'outline', 'ghost'], 'solid');
        $size = $this->allow($size, ['sm', 'md', 'lg'], 'md');

        return $this->classes(array_merge([
            'myron-button',
            'myron-button--intent-' . $intent,
            'myron-button--appearance-' . $appearance,
            'myron-button--size-' . $size,
        ], $extraClasses));
    }

    /**
     * @param list<string> $extraClasses
     */
    public function cardClasses(string $tone, string $border, string $padding, array $extraClasses = []): string
    {
        $tone = $this->allow($tone, ['default', 'alt'], 'default');
        $border = $this->allow($border, ['thin'], 'thin');
        $padding = $this->allow($padding, ['sm', 'md', 'lg'], 'md');

        return $this->classes(array_merge([
            'myron-card',
            'myron-card--tone-' . $tone,
            'myron-card--border-' . $border,
            'myron-card--padding-' . $padding,
        ], $extraClasses));
    }

    /**
     * @param list<string> $extraClasses
     */
    public function sectionBandClasses(string $tone, string $padding, string $variant, array $extraClasses = []): string
    {
        $tone = $this->allow($tone, ['default', 'alt'], 'default');
        $padding = $this->allow($padding, ['sm', 'md', 'lg'], 'md');
        $variant = $this->slug($variant !== '' ? $variant : 'default');

        return $this->classes(array_merge([
            'myron-section-band',
            'myron-section-band--tone-' . $tone,
            'myron-section-band--padding-y-' . $padding,
            'myron-section-band--' . $variant,
        ], $extraClasses));
    }

    /**
     * @param list<string> $extraClasses
     */
    public function heroSimpleClasses(string $variant, string $layout, array $extraClasses = []): string
    {
        $layout = $this->allow($layout, ['centered', 'split'], 'split');
        $variant = $this->slug($variant !== '' ? $variant : 'default');

        return $this->classes(array_merge([
            'myron-hero-simple',
            'myron-hero-simple--layout-' . $layout,
            'myron-section-band',
            'myron-section-band--tone-default',
            'myron-section-band--padding-y-lg',
            'myron-public-component',
            'myron-public-component--hero-simple',
            'myron-hero-simple--' . $variant,
        ], $extraClasses));
    }

    /**
     * @param list<string> $extraClasses
     */
    public function heroWithCtaClasses(string $variant, array $extraClasses = []): string
    {
        $variant = $this->slug($variant !== '' ? $variant : 'default');

        $classes = [
            'myron-hero-with-cta',
            'myron-hero-simple',
            'myron-section-band',
            'myron-section-band--tone-default',
            'myron-section-band--padding-y-lg',
            'myron-public-component',
            'myron-public-component--hero-with-cta',
            'myron-hero-with-cta--' . $variant,
        ];
        if ($variant !== 'cinematic') {
            $classes[] = 'myron-hero-simple--layout-split';
        }

        return $this->classes(array_merge($classes, $extraClasses));
    }

    /**
     * @param list<string> $extraClasses
     */
    public function featureGridClasses(string $variant, string $density, array $extraClasses = []): string
    {
        $density = $this->allow($density, ['sm', 'md', 'lg'], 'md');
        $variant = $this->slug($variant !== '' ? $variant : 'default');

        return $this->classes(array_merge([
            'myron-feature-grid',
            'myron-feature-grid--density-' . $density,
            'myron-feature-grid--' . $variant,
            'myron-public-component',
            'myron-public-component--feature-grid',
        ], $extraClasses));
    }

    /**
     * @param list<string> $classes
     */
    private function classes(array $classes): string
    {
        $clean = [];
        foreach ($classes as $class) {
            $class = trim($class);
            if ($class !== '') {
                $clean[] = $class;
            }
        }

        return implode(' ', array_values(array_unique($clean)));
    }

    /**
     * @param list<string> $allowed
     */
    private function allow(string $value, array $allowed, string $fallback): string
    {
        $value = $this->slug($value);
        return in_array($value, $allowed, true) ? $value : $fallback;
    }

    private function slug(string $value): string
    {
        $slug = strtolower(trim($value));
        $slug = preg_replace('~[^a-z0-9]+~', '-', $slug) ?: '';
        $slug = trim($slug, '-');

        return $slug !== '' ? $slug : 'default';
    }
}
