<?php

namespace Mtc\Plugins\BlockManager\Services;

use Mtc\Plugins\BlockManager\Models\BlockManagerVariantDraft;

/**
 * Emits the Twig partial body for a Designer-published variant.
 *
 * Output mirrors the shape of the hand-authored Phase 2 variants
 * (plugins/BlockManager/elements/{type}/variants/{variant}.twig) but with
 * the variant's slug embedded in the BEM modifier class. The Composer's
 * ship pipeline includes these partials when rendering composed blocks.
 */
class VariantTwigGenerator
{
    public function generate(BlockManagerVariantDraft $draft): string
    {
        $type = $draft->element_type;
        $slug = $draft->variant_slug;

        return $this->template($type, $slug);
    }

    private function template(string $type, string $slug): string
    {
        switch ($type) {
            case 'heading':
                return implode("\n", [
                    "{# Heading — {$slug} variant (Designer-authored). Expects: { text: string, level: 'h1'..'h6' } #}",
                    "{% set tag = level|default('h2') %}",
                    "<{{ tag }} class=\"bm-heading bm-heading--{$slug}\">{{ text }}</{{ tag }}>",
                    '',
                ]);

            case 'paragraph':
                return implode("\n", [
                    "{# Paragraph — {$slug} variant (Designer-authored). Expects: { text: string } #}",
                    "<p class=\"bm-paragraph bm-paragraph--{$slug}\">{{ text|nl2br }}</p>",
                    '',
                ]);

            case 'button':
                return implode("\n", [
                    "{# Button — {$slug} variant (Designer-authored). Expects: { label, link, target } #}",
                    "{% set href = (link.url is defined) ? link.url : (link|default('#')) %}",
                    "<a class=\"bm-button bm-button--{$slug}\" href=\"{{ href }}\" target=\"{{ target|default('_self') }}\">{{ label }}</a>",
                    '',
                ]);

            case 'link':
                return implode("\n", [
                    "{# Link — {$slug} variant (Designer-authored). Expects: { text, link, target } #}",
                    "{% set href = (link.url is defined) ? link.url : (link|default('#')) %}",
                    "<a class=\"bm-link bm-link--{$slug}\" href=\"{{ href }}\" target=\"{{ target|default('_self') }}\">{{ text }}</a>",
                    '',
                ]);

            case 'details':
                return implode("\n", [
                    "{# Details — {$slug} variant (Designer-authored). Expects: { summary, body } #}",
                    "<details class=\"bm-details bm-details--{$slug}\">",
                    "    <summary>{{ summary }}</summary>",
                    "    <div class=\"bm-details__body\">{{ body|nl2br }}</div>",
                    "</details>",
                    '',
                ]);

            case 'table':
                return implode("\n", [
                    "{# Table — {$slug} variant (Designer-authored). Expects: { caption, headers (csv), rows (lines of | separated cells) } #}",
                    "<table class=\"bm-table bm-table--{$slug}\">",
                    "    {% if caption %}<caption>{{ caption }}</caption>{% endif %}",
                    "    {% if headers %}<thead><tr>{% for h in headers|split(',') %}<th>{{ h|trim }}</th>{% endfor %}</tr></thead>{% endif %}",
                    "    {% if rows %}<tbody>{% for line in rows|split('\\n') %}{% if line|trim != '' %}<tr>{% for cell in line|split('|') %}<td>{{ cell|trim }}</td>{% endfor %}</tr>{% endif %}{% endfor %}</tbody>{% endif %}",
                    "</table>",
                    '',
                ]);

            case 'hr':
                return implode("\n", [
                    "{# HR — {$slug} variant (Designer-authored). Expects: { aria_label } #}",
                    "<hr class=\"bm-hr bm-hr--{$slug}\"{% if aria_label %} aria-label=\"{{ aria_label }}\"{% endif %}>",
                    '',
                ]);

            case 'image':
                return implode("\n", [
                    "{# Image — {$slug} variant (Designer-authored). Expects: { src, alt, width, height } #}",
                    "<img class=\"bm-image bm-image--{$slug}\" src=\"{{ src }}\" alt=\"{{ alt|default('') }}\"{% if width %} width=\"{{ width }}\"{% endif %}{% if height %} height=\"{{ height }}\"{% endif %}>",
                    '',
                ]);

            default:
                return implode("\n", [
                    "{# {$type} — {$slug} variant (Designer-authored). #}",
                    "<div class=\"bm-{$type} bm-{$type}--{$slug}\">{{ text|default('') }}</div>",
                    '',
                ]);
        }
    }
}
