<?php

declare(strict_types=1);

require_once __DIR__ . '/invoice_contract.php';
require_once __DIR__ . '/invoice_pdf_content.php';

function invoice_payment_amount_header_html(string $suffix): string
{
    return '<th class="invoice-doc-amount-header"><span>Betrag</span><span>(' . e($suffix) . ')</span></th>';
}

function render_invoice_contract_document_html(array $contract): string
{
    $mode = (string)($contract['invoice_mode'] ?? 'website');
    $totals = invoice_contract_totals($contract);
    $pos1Schedule = invoice_payment_plan_pos1_schedule($contract, $totals);
    $pos2Rows = invoice_should_show_payment_plan_pos2($contract) ? invoice_maintenance_plan_rows($contract) : [];
    $tseRows = invoice_should_show_payment_plan_pos3($contract) ? invoice_tse_monthly_plan_rows($contract) : [];
    $offerNo = invoice_pdf_offer_number($contract);
    $optionalItem = is_array($contract['optional_item'] ?? null)
        ? $contract['optional_item']
        : invoice_contract_mode_config($mode)['optional_item'];

    ob_start();
    ?>
<article class="contract-doc invoice-contract-doc">
    <div class="contract-doc-hero">
        <h1 class="contract-doc-title"><?= e(invoice_contract_pdf_title($contract)) ?></h1>
    </div>

    <section class="contract-doc-block">
        <div class="invoice-doc-customer-lines">
            <?php foreach (invoice_customer_detail_lines($contract) as $line): ?>
            <div class="invoice-doc-customer-line"><?= e($line) ?></div>
            <?php endforeach; ?>
        </div>
    </section>

    <section class="contract-doc-block">
        <div class="invoice-doc-offer-meta invoice-doc-offer-meta-row">
            <strong class="invoice-doc-offer-meta-main"><?= e('Angebot Nr. ' . $offerNo) ?></strong>
            <div class="invoice-doc-offer-meta-dates">
                <div class="invoice-doc-offer-meta-date">
                    <span class="invoice-doc-offer-meta-label"><?= e('Angebotsdatum') ?></span>
                    <span class="invoice-doc-meta-value"><?= e(invoice_pdf_format_date((string)($contract['contract_date'] ?? ''))) ?></span>
                </div>
                <div class="invoice-doc-offer-meta-date">
                    <span class="invoice-doc-offer-meta-label"><?= e('Gültig bis') ?></span>
                    <span class="invoice-doc-meta-value"><?= e(invoice_pdf_valid_until_date($contract)) ?></span>
                </div>
            </div>
        </div>
    </section>

    <section class="contract-doc-block">
        <p class="contract-doc-meta"><?= e(invoice_pdf_intro_text($contract)) ?></p>
        <h2 class="contract-doc-block-title contract-doc-block-title-plain"><?= e(__('invoice_services_line_items')) ?></h2>
        <div class="invoice-doc-table-wrap">
            <table class="invoice-doc-table">
                <thead>
                    <tr>
                        <th><?= e('Pos.') ?></th>
                        <th><?= e(__('invoice_col_description')) ?></th>
                        <th><?= e(__('invoice_col_quantity')) ?></th>
                        <th><?= e(__('invoice_col_unit_price')) ?></th>
                        <th><?= e(__('invoice_col_total')) ?></th>
                    </tr>
                </thead>
                <tbody>
                    <?php $pos = 1; ?>
                    <?php foreach ($contract['line_items'] ?? [] as $item): ?>
                    <tr class="invoice-doc-product-row">
                        <td><?= e((string)$pos++) ?></td>
                        <td class="invoice-doc-product-label"><?= e((string)($item['description'] ?? '')) ?></td>
                        <td><?= e((string)($item['quantity'] ?? 1)) ?></td>
                        <td class="invoice-doc-product-amount"><?= e(invoice_format_money((float)($item['unit_price_net'] ?? 0))) ?></td>
                        <td class="invoice-doc-product-amount"><?= e(invoice_format_money((float)($item['total_net'] ?? 0))) ?></td>
                    </tr>
                    <?php endforeach; ?>
                    <?php if (!empty($contract['optional_printer']) && is_array($optionalItem)): ?>
                    <tr class="invoice-doc-product-row">
                        <td><?= e((string)$pos++) ?></td>
                        <td class="invoice-doc-product-label"><?= e((string)($optionalItem['description'] ?? '')) ?></td>
                        <td>1</td>
                        <td class="invoice-doc-product-amount"><?= e(invoice_format_money((float)($optionalItem['unit_price_net'] ?? 0))) ?></td>
                        <td class="invoice-doc-product-amount"><?= e(invoice_format_money((float)($optionalItem['unit_price_net'] ?? 0))) ?></td>
                    </tr>
                    <?php endif; ?>
                </tbody>
            </table>
        </div>

        <div class="invoice-doc-totals">
            <?php $totalAlt = false; ?>
            <?php foreach (invoice_document_totals_rows($totals) as $row): ?>
            <?php
            $rowClasses = ['invoice-doc-total-line'];
            if ($totalAlt) {
                $rowClasses[] = 'invoice-doc-total-line-alt';
            }
            if (!empty($row['discount'])) {
                $rowClasses[] = 'invoice-doc-total-discount';
            } elseif (!empty($row['net_total'])) {
                $rowClasses[] = 'invoice-doc-total-net-total';
            } elseif (!empty($row['rule_after'])) {
                $rowClasses[] = 'invoice-doc-total-final';
            } elseif (!empty($row['rule_before'])) {
                $rowClasses[] = 'invoice-doc-total-vat-row';
            }
            $totalAlt = !$totalAlt;
            ?>
            <div class="<?= e(implode(' ', $rowClasses)) ?>">
                <span><?= e((string)$row['label']) ?></span>
                <strong><?= e(invoice_format_totals_display_amount($row)) ?></strong>
            </div>
            <?php endforeach; ?>
        </div>
        <p class="invoice-doc-fixed-price-notice"><?= e(invoice_pdf_fixed_price_notice()) ?></p>
    </section>

    <section class="contract-doc-block invoice-doc-page-break">
        <h2 class="contract-doc-block-title contract-doc-block-title-plain">1.1 Zahlungsplan</h2>
        <p class="contract-doc-copy"><?= e(invoice_pdf_payment_plan_intro($contract, $totals)) ?></p>

        <h3 class="invoice-doc-plan-subtitle"><?= e(invoice_payment_plan_pos1_title($contract)) ?></h3>
        <div class="invoice-doc-table-wrap">
            <table class="invoice-doc-table invoice-doc-plan-table">
                <thead>
                    <tr>
                        <th>Nr.</th>
                        <th>Fälligkeit</th>
                        <?= invoice_payment_amount_header_html('Netto') ?>
                        <?= invoice_payment_amount_header_html('Brutto') ?>
                    </tr>
                </thead>
                <tbody>
                    <?php foreach ($pos1Schedule as $row): ?>
                    <tr>
                        <td><?= e((string)($row['number'] ?? '')) ?></td>
                        <td><?= e((string)($row['due_label'] ?? '')) ?></td>
                        <td><?= e(invoice_format_money((float)($row['net'] ?? 0))) ?></td>
                        <td><?= e(invoice_format_money((float)($row['gross'] ?? 0))) ?></td>
                    </tr>
                    <?php endforeach; ?>
                </tbody>
            </table>
        </div>

        <?php if ($pos2Rows !== []): ?>
        <h3 class="invoice-doc-plan-subtitle"><?= e(invoice_payment_plan_pos2_title()) ?></h3>
        <div class="invoice-doc-table-wrap">
            <table class="invoice-doc-table invoice-doc-plan-table">
                <thead>
                    <tr>
                        <th>Fälligkeit</th>
                        <?= invoice_payment_amount_header_html('Netto') ?>
                        <?= invoice_payment_amount_header_html('Brutto') ?>
                    </tr>
                </thead>
                <tbody>
                    <?php foreach ($pos2Rows as $row): ?>
                    <tr>
                        <td><?= e((string)($row['due'] ?? '')) ?></td>
                        <td><?= e((string)($row['net'] ?? '')) ?></td>
                        <td><?= e(invoice_format_money((float)($row['gross'] ?? 0))) ?></td>
                    </tr>
                    <?php endforeach; ?>
                </tbody>
            </table>
        </div>
        <?php endif; ?>

        <?php if ($tseRows !== []): ?>
        <h3 class="invoice-doc-plan-subtitle"><?= e(invoice_payment_plan_tse_title()) ?></h3>
        <div class="invoice-doc-table-wrap">
            <table class="invoice-doc-table invoice-doc-plan-table">
                <thead>
                    <tr>
                        <th>Fälligkeit</th>
                        <?= invoice_payment_amount_header_html('Netto') ?>
                        <?= invoice_payment_amount_header_html('Brutto') ?>
                    </tr>
                </thead>
                <tbody>
                    <?php foreach ($tseRows as $row): ?>
                    <tr>
                        <td><?= e((string)($row['due'] ?? '')) ?></td>
                        <td><?= e((string)($row['net'] ?? '')) ?></td>
                        <td><?= e(invoice_format_money((float)($row['gross'] ?? 0))) ?></td>
                    </tr>
                    <?php endforeach; ?>
                </tbody>
            </table>
        </div>
        <?php endif; ?>
    </section>

    <section class="contract-doc-block invoice-doc-terms">
        <?php foreach (invoice_document_terms_sections($contract) as $section): ?>
        <div class="invoice-doc-term">
            <h3 class="invoice-doc-term-title"><?= e((string)($section['heading'] ?? '')) ?></h3>
            <?php if (!empty($section['intro'])): ?>
            <p class="contract-doc-copy invoice-doc-term-body"><?= e((string)$section['intro']) ?></p>
            <?php endif; ?>
            <?php if (!empty($section['items']) && is_array($section['items'])): ?>
            <ul class="invoice-doc-term-list invoice-doc-term-body">
                <?php foreach ($section['items'] as $item): ?>
                <li>
                    <strong><?= e((string)($item['title'] ?? '')) ?></strong>
                    <span><?= e((string)($item['text'] ?? '')) ?></span>
                </li>
                <?php endforeach; ?>
            </ul>
            <?php endif; ?>
            <?php if (!empty($section['body'])): ?>
            <p class="contract-doc-copy whitespace-pre-line invoice-doc-term-body"><?= e((string)$section['body']) ?></p>
            <?php endif; ?>
        </div>
        <?php endforeach; ?>

        <div class="invoice-doc-closing">
            <?php foreach (invoice_pdf_closing_lines() as $line): ?>
            <p><?= e($line) ?></p>
            <?php endforeach; ?>
        </div>
    </section>
</article>
    <?php
    return (string)ob_get_clean();
}
