<?php

declare(strict_types=1);

require_once __DIR__ . '/contract_form.php';

function registration_owner_count(array $contract): int
{
    return contract_normalize_owner_count($contract['number_of_owners'] ?? 1);
}

function registration_owner_names(array $contract): array
{
    if (!empty($contract['owner_names']) && is_array($contract['owner_names'])) {
        $names = [];
        foreach ($contract['owner_names'] as $name) {
            $name = trim((string)$name);
            if ($name !== '') {
                $names[] = $name;
            }
        }

        if ($names !== []) {
            return array_slice($names, 0, registration_owner_count($contract));
        }
    }

    $ownerName = trim((string)($contract['owner_name'] ?? ''));
    if ($ownerName !== '') {
        return array_map('trim', explode(',', $ownerName));
    }

    return array_fill(0, registration_owner_count($contract), '');
}

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

    if (invoice_contract_is($contract)) {
        return invoice_registration_summary_rows($contract);
    }

    $rows = [
        'field_restaurant_name' => $contract['restaurant_name'] ?? '',
        'field_brand' => $contract['brand'] ?? '',
        'field_contract_date' => format_date($contract['contract_date'] ?? null),
        'field_legal_name' => $contract['legal_name'] ?? '',
        'field_owner_name' => contract_owner_name_for_pdf($contract),
        'field_contact_person' => $contract['contact_person'] ?? '',
        'field_company_phone' => $contract['company_phone'] ?? '',
        'field_contact_mobile' => $contract['contact_mobile'] ?? '',
        'field_street' => $contract['street'] ?? '',
        'field_postal_city' => $contract['postal_city'] ?? '',
        'field_country' => $contract['country'] ?? '',
        'field_vat_id' => $contract['vat_id'] ?? '',
        'field_tax_number' => $contract['tax_number'] ?? '',
    ];

    $filtered = [];
    foreach ($rows as $key => $value) {
        $value = trim((string)$value);
        if ($value !== '' && $value !== '-') {
            $filtered[$key] = $value;
        }
    }

    return $filtered;
}

function invoice_registration_summary_rows(array $contract): array
{
    require_once __DIR__ . '/invoice_pdf_content.php';

    $totals = invoice_contract_totals($contract);
    $installmentSchedule = invoice_payment_plan_pos1_schedule($contract, $totals);

    $rows = [
        'field_brand' => (string)($contract['contract_line'] ?? ''),
        'field_contract_date' => format_date($contract['contract_date'] ?? null),
        'invoice_offer_number' => invoice_pdf_offer_number($contract),
        'invoice_installment_months' => (string)count($installmentSchedule),
        'invoice_monthly_installment' => invoice_format_money((float)invoice_installment_info($contract, $totals)['monthly_total']),
        'invoice_total_amount' => invoice_format_money((float)$totals['total']),
        'invoice_delivery_date' => invoice_pdf_delivery_date($contract),
    ];

    $customerLabel = invoice_customer_label((string)($contract['customer_details'] ?? ''));
    if ($customerLabel !== 'Customer') {
        $rows['field_restaurant_name'] = $customerLabel;
    }

    $filtered = [];
    foreach ($rows as $key => $value) {
        $value = trim((string)$value);
        if ($value !== '' && $value !== '-') {
            $filtered[$key] = $value;
        }
    }

    return $filtered;
}

function registration_completed_signature_count(array $contract, array $signatures): int
{
    $token = $contract['token'] ?? '';
    $count = 0;

    foreach ($signatures as $signature) {
        if (($signature['contract_token'] ?? '') !== $token) {
            continue;
        }
        $count++;
    }

    return $count;
}

function registration_is_fully_signed(array $contract, array $signatures): bool
{
    if (($contract['status'] ?? '') !== 'signed') {
        return false;
    }

    return registration_completed_signature_count($contract, $signatures) >= registration_owner_count($contract);
}

function registration_legal_paragraphs(): array
{
    return [
        'reg_legal_p1',
        'reg_legal_p2',
    ];
}

function registration_consent_items(): array
{
    return [
        'reg_consent_accuracy',
        'reg_consent_privacy',
        'reg_consent_electronic',
    ];
}
