<?php

declare(strict_types=1);

require_once __DIR__ . '/i18n.php';

function contract_template_locale(): string
{
    $locale = $GLOBALS['karvi_locale'] ?? current_locale();

    return in_array($locale, ['en', 'de'], true) ? $locale : 'en';
}

function contract_template_strings(): array
{
    static $cache = [];

    $locale = contract_template_locale();
    if (!isset($cache[$locale])) {
        $file = __DIR__ . '/../lang/contract_' . $locale . '.php';
        $cache[$locale] = is_file($file) ? require $file : require __DIR__ . '/../lang/contract_de.php';
    }

    return $cache[$locale];
}

function contract_t(string $key, string $default = ''): string
{
    $value = contract_template_strings()[$key] ?? $default;

    return is_string($value) ? $value : $default;
}

function contract_display_date(?string $date): string
{
    if (!$date) {
        return '___ / ___ / ______';
    }

    $timestamp = strtotime($date);
    if ($timestamp === false) {
        return $date;
    }

    if (contract_template_locale() === 'de') {
        return date('d.m.Y', $timestamp);
    }

    return date('F j, Y', $timestamp);
}

function contract_display_date_short(?string $date): string
{
    if (!$date) {
        return '___-__-____';
    }

    $timestamp = strtotime($date);
    if ($timestamp === false) {
        return $date;
    }

    return date('d-m-Y', $timestamp);
}

function contract_template_karvi(): array
{
    return [
        'name' => 'Karvi GmbH',
        'street' => 'Große Gallusstraße 14',
        'city' => '60315 Frankfurt am Main',
        'country' => 'Deutschland',
        'phone' => '0711 252417 70',
        'email' => 'info@karviparner.de',
        'web' => 'www.karviparner.de',
        'register' => 'HRB 143175',
        'owner' => 'Sharma Kartik',
        'signatory_name' => 'Sharma Kartik',
        'signatory_title' => contract_t('karvi_signatory_title', 'Eigentümer'),
    ];
}

function contract_template_title(): string
{
    return contract_t('title');
}

function contract_template_opening(): string
{
    return contract_t('opening');
}

function contract_template_partner_fields(): array
{
    return contract_template_strings()['partner_fields'] ?? [];
}

function contract_template_commission_platforms(): array
{
    return contract_template_strings()['commission_platforms'] ?? [];
}

function contract_template_commission_rows(): array
{
    $rows = contract_template_strings()['commission_rows'] ?? [];
    $rows['platform_delivery_pct'] = null;

    return $rows;
}

function contract_template_technical_services(): array
{
    return contract_template_strings()['technical_services'] ?? [];
}

function contract_template_additional_conditions(): string
{
    return contract_t('additional_conditions');
}

function contract_template_legal_clauses(): array
{
    return contract_template_strings()['legal_clauses'] ?? [];
}

function contract_template_clause_41_intro(): string
{
    return contract_t('clause_41_intro');
}

function contract_template_clause_41(array $contract): string
{
    $selected = contract_selected_brands($contract);
    $isGulab = in_array('Gulab', $selected, true);
    $isFlamers = in_array('Flamers', $selected, true);

    $gulabMark = $isGulab ? '[X]' : '[ ]';
    $flamersMark = $isFlamers ? '[X]' : '[ ]';

    return contract_template_clause_41_intro() . "\n"
        . $gulabMark . ' Gulab    ' . $flamersMark . ' Flamers Pizza';
}

function contract_template_sample_data(): array
{
    return [
        'token' => 'template_preview',
        'id' => 0,
        'status' => 'template',
        'contract_date' => '2026-05-30',
        'number_of_owners' => '1',
        'brands' => ['Flamers'],
        'brand' => 'Flamers',
        'owner_names' => ['Dharminder Singh Virk'],
        'restaurant_name' => 'Pizzeria Roma',
        'email' => 'flamers@karvipartner.de',
        'legal_name' => 'Pizzeria Roma',
        'owner_name' => 'Dharminder Singh Virk',
        'contact_person' => 'Dharminder Singh Virk',
        'company_phone' => '+4917621391255',
        'contact_mobile' => '+491785524215',
        'street' => 'Hans-Geiger-Straße 1',
        'postal_city' => '91052 Erlangen',
        'country' => 'Deutschland',
        'commercial_register' => 'HRB 143175',
        'vat_id' => 'XXX',
        'tax_number' => 'XXX',
        'tax_identification' => 'XXX',
        'lieferando_own_delivery_pct' => '15',
        'lieferando_platform_delivery_pct' => '',
        'lieferando_pickup_pct' => '15',
        'lieferando_online_payment_fee' => '0.64',
        'wolt_own_delivery_pct' => '',
        'wolt_platform_delivery_pct' => '',
        'wolt_pickup_pct' => '',
        'wolt_online_payment_fee' => '',
        'uber_own_delivery_pct' => '',
        'uber_platform_delivery_pct' => '',
        'uber_pickup_pct' => '',
        'uber_online_payment_fee' => '',
    ];
}
