<?php

declare(strict_types=1);

require_once __DIR__ . '/helpers.php';
require_once __DIR__ . '/contract_form.php';
require_once __DIR__ . '/contract_template.php';
require_once __DIR__ . '/pdf_helpers.php';

function render_contract_document_html(array $contract): string
{
    require_once __DIR__ . '/invoice_contract.php';

    if (invoice_contract_is($contract)) {
        require_once __DIR__ . '/invoice_contract_document_html.php';

        return render_invoice_contract_document_html($contract);
    }

    require_once __DIR__ . '/pdf_generator.php';

    $karvi = contract_template_karvi();
    $opening = str_replace(
        '{contract_date}',
        contract_display_date($contract['contract_date'] ?? null),
        contract_template_opening()
    );
    $selectedBrands = contract_selected_brands($contract);
    $brandOptions = [
        ['key' => 'Gulab', 'label' => 'Gulab'],
        ['key' => 'Flamers', 'label' => 'Flamers Pizza'],
    ];

    ob_start();
    ?>
<article class="contract-doc">
    <div class="contract-doc-hero">
        <h1 class="contract-doc-title"><?= e(contract_template_title()) ?></h1>
        <p class="contract-doc-meta"><?= e($opening) ?></p>
    </div>

    <p class="contract-doc-divider"><?= e(contract_t('between')) ?></p>

    <section class="contract-doc-block">
        <h2 class="contract-doc-block-title">
            <span class="contract-doc-block-num">1</span>
            <?= e(contract_t('section_1_heading')) ?>
        </h2>
        <div class="contract-doc-copy">
            <p><?= e($karvi['name']) ?></p>
            <p><?= e($karvi['street']) ?></p>
            <p><?= e($karvi['city']) ?></p>
            <p><?= e($karvi['country']) ?></p>
            <p class="contract-doc-note"><?= e(contract_t('karvi_hereinafter')) ?></p>
        </div>
    </section>

    <p class="contract-doc-divider"><?= e(contract_t('and')) ?></p>

    <section class="contract-doc-block">
        <h2 class="contract-doc-block-title">
            <span class="contract-doc-block-num">2</span>
            <?= e(contract_t('section_2_heading')) ?>
        </h2>
        <dl class="contract-doc-fields">
            <?php foreach (contract_template_partner_fields() as $key => $label): ?>
            <div class="contract-doc-field">
                <dt><?= e($label) ?></dt>
                <dd><?= e(pdf_partner_field_value($contract, $key)) ?></dd>
            </div>
            <?php endforeach; ?>
        </dl>
    </section>

    <section class="contract-doc-block">
        <h2 class="contract-doc-block-title">
            <span class="contract-doc-block-num">3</span>
            <?= e(contract_t('section_3_heading')) ?>
        </h2>

        <?php foreach (contract_template_commission_platforms() as $platform => $meta): ?>
        <div class="contract-doc-panel">
            <h3 class="contract-doc-panel-title"><?= e($meta['heading']) ?></h3>
            <ul class="contract-doc-rows">
                <?php foreach (contract_template_commission_rows() as $suffix => $label): ?>
                <?php
                if ($suffix === 'platform_delivery_pct') {
                    $label = $meta['platform_delivery_label'];
                }
                $field = $platform . '_' . $suffix;
                $value = $contract[$field] ?? '';
                $display = str_ends_with($suffix, '_pct')
                    ? pdf_format_pct((string)$value)
                    : pdf_format_fee((string)$value);
                ?>
                <li>
                    <span class="contract-doc-row-label"><?= e($label) ?></span>
                    <span class="contract-doc-row-value"><?= e($display) ?></span>
                </li>
                <?php endforeach; ?>
            </ul>
        </div>
        <?php endforeach; ?>

        <h3 class="contract-doc-mini-title"><?= e(contract_t('technical_services_heading')) ?></h3>
        <ul class="contract-doc-bullets">
            <?php foreach (contract_template_technical_services() as $service): ?>
            <li><?= e($service) ?></li>
            <?php endforeach; ?>
        </ul>

        <h3 class="contract-doc-mini-title"><?= e(contract_t('additional_conditions_heading')) ?></h3>
        <p class="contract-doc-copy"><?= e(contract_template_additional_conditions()) ?></p>
    </section>

    <section class="contract-doc-block">
        <h2 class="contract-doc-block-title">
            <span class="contract-doc-block-num">4</span>
            <?= e(contract_t('section_4_heading')) ?>
        </h2>

        <div class="contract-doc-item">
            <span class="contract-doc-item-num">4.1</span>
            <div class="contract-doc-item-body">
                <p><?= e(contract_template_clause_41_intro()) ?></p>
                <ul class="contract-doc-tags">
                    <?php foreach ($brandOptions as $brand):
                        $isSelected = in_array($brand['key'], $selectedBrands, true);
                    ?>
                    <li class="<?= $isSelected ? 'is-active' : '' ?>">
                        <?php if ($isSelected): ?>
                        <span class="contract-doc-brand-check material-symbols-rounded" aria-hidden="true">check</span>
                        <?php endif; ?>
                        <span><?= e($brand['label']) ?></span>
                    </li>
                    <?php endforeach; ?>
                </ul>
            </div>
        </div>

        <?php foreach (contract_template_legal_clauses() as $number => $text): ?>
        <?php if ($number === '4.20'): ?>
        <h3 class="contract-doc-mini-title"><?= e(contract_t('authorization_heading')) ?></h3>
        <?php endif; ?>
        <div class="contract-doc-item">
            <span class="contract-doc-item-num"><?= e($number) ?></span>
            <p class="contract-doc-item-body"><?= e($text) ?></p>
        </div>
        <?php endforeach; ?>
    </section>
</article>
    <?php

    return (string)ob_get_clean();
}
