<?php
/**
 * Hyvä Themes - https://hyva.io
 */
declare(strict_types=1);

use Hyva\Theme\Model\ViewModelRegistry;
use Hyva\Theme\ViewModel\CurrentProduct;
use Magento\Framework\Escaper;
use Magento\Framework\View\Element\Template;

/** @var Template $block */
/** @var Escaper $escaper */
/** @var ViewModelRegistry $viewModels */

$currentProduct = $viewModels->require(CurrentProduct::class);
$product = $block->hasData('product') ? $block->getData('product') : $currentProduct->get();

if (!$product || !$product->getId()) {
    return;
}

$faqRaw = $product->getData('qa'); // Admin textarea

if (!empty($faqRaw) && strtolower(trim($faqRaw)) !== 'no'):
    $faqItems = [];
    $lines = preg_split('/\r\n|\r|\n/', $faqRaw);
    $currentQuestion = '';
    $currentAnswer = '';
    $parsingQuestion = false;

    foreach ($lines as $line) {
        $trimmed = trim($line);
        if (stripos($trimmed, 'Q:') === 0) {
            $currentQuestion = trim(substr($trimmed, 2));
            $currentAnswer = '';
            $parsingQuestion = true;
        } elseif (stripos($trimmed, 'A:') === 0) {
            $currentAnswer = trim(substr($trimmed, 2));
            $parsingQuestion = false;
        } else {
            if ($parsingQuestion) {
                $currentQuestion .= ' ' . $trimmed;
            } else {
                $currentAnswer .= ' ' . $trimmed;
            }
        }

        if ($currentQuestion && $currentAnswer) {
            $faqItems[] = [
                'question' => $currentQuestion,
                'answer'   => $currentAnswer
            ];
            $currentQuestion = '';
            $currentAnswer = '';
        }
    }
    ?>
    <?php if (!empty($faqItems)): ?>
    <?php $total = count($faqItems); ?>

    <div x-data="{ showAll: false }" class="">
        <?php foreach ($faqItems as $index => $item): ?>
            <?php
            $isLast = $index === $total - 1;
            $itemClasses = 'rounded';
            if (!$isLast) {
                $itemClasses .= ' border-b border-gray-300 mb-3';
            }
            ?>
            <div
                x-data="{ open: false }"
                class="<?= $itemClasses ?>"
                x-show="showAll || <?= $index ?> < 5"
                x-transition
            >
                <div
                    @click="open = !open"
                    class="flex items-start p-2 cursor-pointer transition"
                >
                    <div
                        class="material-symbols-outlined text-xl font-bold w-6 mr-2"
                        x-text="open ? '-' : '+'"
                    ></div>
                    <div class="font-semibold text-gray-800">
                        <?= $escaper->escapeHtml($item['question']) ?>
                    </div>
                </div>
                <div
                    x-show="open"
                    x-transition
                    class="p-4 pt-0 text-gray-700 sm:ml-6 mt-2"
                >
                    <?= nl2br($escaper->escapeHtml($item['answer'])) ?>
                </div>
            </div>
        <?php endforeach; ?>

        <?php if (count($faqItems) > 5): ?>
            <div class="flex justify-center mt-4">
                <button
                    @click="showAll = !showAll"
                    class="bg-red-50 border border-red-600 font-medium hover:bg-red-100 px-6 py-2 rounded-xl text-red-600 transition"
                    x-text="showAll ? 'Show less' : 'Show more questions'"
                >
                    Show more questions
                </button>
            </div>

        <?php endif; ?>
    </div>
<?php endif; ?>
<?php endif; ?>
