<?php

declare(strict_types=1);

function pdf_format_pct(?string $value): string
{
    $value = trim((string)$value);
    if ($value === '') {
        return '—';
    }

    return str_replace('.', ',', $value) . ' %';
}

function pdf_format_fee(?string $value): string
{
    $value = trim((string)$value);
    if ($value === '') {
        return '—';
    }

    if (stripos($value, 'eur') !== false) {
        return $value;
    }

    return str_replace('.', ',', $value) . ' € pro Bestellung';
}

function pdf_field_value(array $contract, string $field): string
{
    $value = trim((string)($contract[$field] ?? ''));

    return $value !== '' ? $value : '—';
}

function pdf_localize_country(string $value): string
{
    $value = trim($value);
    if ($value === '') {
        return '—';
    }

    $map = [
        'Germany' => 'Deutschland',
        'GERMANY' => 'Deutschland',
        'germany' => 'Deutschland',
        'German' => 'Deutschland',
        'Austria' => 'Österreich',
        'Switzerland' => 'Schweiz',
    ];

    return $map[$value] ?? $value;
}

function pdf_partner_field_value(array $contract, string $field): string
{
    if ($field === 'brand') {
        $brands = contract_selected_brands($contract);
        $display = contract_brand_display_names($brands);

        return $display !== '' ? $display : '—';
    }

    if ($field === 'country') {
        return pdf_localize_country((string)($contract['country'] ?? ''));
    }

    if ($field === 'owner_name') {
        $value = contract_owner_name_for_pdf($contract);

        return $value !== '' ? $value : '—';
    }

    return pdf_field_value($contract, $field);
}
