<?php
declare(strict_types=1);

function order_option_is_film_service_name(string $serviceName): bool
{
    $serviceName = trim($serviceName);

    if ($serviceName === '') {
        return false;
    }

    $lowerName = strtolower($serviceName);
    $keywords = array(
        'tint',
        'window film',
        'window tint',
        json_decode('"\u0e1f\u0e34\u0e25\u0e4c\u0e21"'),
        json_decode('"\u0e01\u0e23\u0e2d\u0e07\u0e41\u0e2a\u0e07"'),
        json_decode('"\u0e15\u0e34\u0e14\u0e1f\u0e34\u0e25\u0e4c\u0e21"'),
    );

    foreach ($keywords as $keyword) {
        if (strpos($lowerName, strtolower((string) $keyword)) !== false || strpos($serviceName, (string) $keyword) !== false) {
            return true;
        }
    }

    return false;
}

function order_option_film_tint_percent_options(): array
{
    return array(
        '' => localized_text_value(json_decode('"\u0e40\u0e25\u0e37\u0e2d\u0e01"'), 'Select'),
        'none' => localized_text_value(json_decode('"\u0e44\u0e21\u0e48\u0e23\u0e30\u0e1a\u0e38\u0e40\u0e1b\u0e2d\u0e23\u0e4c\u0e40\u0e0b\u0e47\u0e19\u0e15\u0e4c"'), 'Not specified'),
        '20' => '20%',
        '30' => '30%',
        '40' => '40%',
        '50' => '50%',
        '60' => '60%',
        '70' => '70%',
        '80' => '80%',
        '90' => '90%',
    );
}

function order_option_detail_lines(?string $serviceOptions): array
{
    $serviceOptions = trim((string) $serviceOptions);

    if ($serviceOptions === '') {
        return array();
    }

    $decoded = json_decode($serviceOptions, true);

    if (!is_array($decoded)) {
        return array();
    }

    $lines = array();

    if (isset($decoded['package_usage']) && is_array($decoded['package_usage'])) {
        $packageUsage = $decoded['package_usage'];
        $packageName = trim((string) ($packageUsage['package_name'] ?? ''));
        $remainingUses = (int) ($packageUsage['remaining_after_use'] ?? 0);
        $line = localized_text_value(json_decode('"\u0e43\u0e0a\u0e49\u0e41\u0e1e\u0e47\u0e01\u0e40\u0e01\u0e08"'), 'Package used');

        if ($packageName !== '') {
            $line .= ': ' . $packageName;
        }

        if ($remainingUses >= 0) {
            $line .= ' / ' . localized_text_value(json_decode('"\u0e04\u0e07\u0e40\u0e2b\u0e25\u0e37\u0e2d"'), 'Remaining') . ' ' . $remainingUses;
        }

        $lines[] = $line;
    }

    if (isset($decoded['package_purchase']) && is_array($decoded['package_purchase'])) {
        $packagePurchase = $decoded['package_purchase'];
        $items = is_array($packagePurchase['items'] ?? null) ? $packagePurchase['items'] : array();

        foreach ($items as $item) {
            $serviceName = trim((string) ($item['service_name'] ?? ''));
            $includedUses = (int) ($item['included_uses'] ?? 0);

            if ($serviceName === '' || $includedUses <= 0) {
                continue;
            }

            $lines[] = localized_text_value(json_decode('"\u0e23\u0e32\u0e22\u0e01\u0e32\u0e23\u0e43\u0e19\u0e41\u0e1e\u0e47\u0e01\u0e40\u0e01\u0e08"'), 'Package item')
                . ': ' . $serviceName
                . ' / ' . $includedUses . ' '
                . localized_text_value(json_decode('"\u0e04\u0e23\u0e31\u0e49\u0e07"'), 'time(s)');
        }
    }

    if ((string) ($decoded['type'] ?? '') !== 'window_tint') {
        return $lines;
    }

    $groupName = trim((string) ($decoded['group_name'] ?? ''));
    if ($groupName !== '') {
        $lines[] = localized_text_value(json_decode('"\u0e01\u0e25\u0e38\u0e48\u0e21\u0e1f\u0e34\u0e25\u0e4c\u0e21"'), 'Film group') . ': ' . $groupName;
    }

    $serviceLabels = array(
        'front_service_name' => localized_text_value(json_decode('"\u0e01\u0e23\u0e30\u0e08\u0e01\u0e2b\u0e19\u0e49\u0e32"'), 'Front windshield'),
        'sunroof_service_name' => localized_text_value('ซันรูฟ', 'Sunroof'),
        'front_side_service_name' => localized_text_value(json_decode('"\u0e01\u0e23\u0e30\u0e08\u0e01\u0e02\u0e49\u0e32\u0e07\u0e04\u0e39\u0e48\u0e2b\u0e19\u0e49\u0e32"'), 'Front side windows'),
        'rear_side_service_name' => localized_text_value(json_decode('"\u0e01\u0e23\u0e30\u0e08\u0e01\u0e02\u0e49\u0e32\u0e07\u0e04\u0e39\u0e48\u0e2b\u0e25\u0e31\u0e07"'), 'Rear side windows'),
        'rear_service_name' => localized_text_value(json_decode('"\u0e01\u0e23\u0e30\u0e08\u0e01\u0e2b\u0e25\u0e31\u0e07"'), 'Rear windshield'),
    );
    $hasServiceNames = false;

    foreach ($serviceLabels as $key => $label) {
        $value = trim((string) ($decoded[$key] ?? ''));

        if ($value === '') {
            continue;
        }

        $hasServiceNames = true;
        $lines[] = $label . ': ' . $value;
    }

    if (!$hasServiceNames) {
        $labels = array(
            'front_percent' => localized_text_value(json_decode('"\u0e01\u0e23\u0e30\u0e08\u0e01\u0e2b\u0e19\u0e49\u0e32"'), 'Front windshield'),
            'sunroof_percent' => localized_text_value('ซันรูฟ', 'Sunroof'),
            'front_side_percent' => localized_text_value(json_decode('"\u0e01\u0e23\u0e30\u0e08\u0e01\u0e02\u0e49\u0e32\u0e07\u0e04\u0e39\u0e48\u0e2b\u0e19\u0e49\u0e32"'), 'Front side windows'),
            'rear_side_percent' => localized_text_value(json_decode('"\u0e01\u0e23\u0e30\u0e08\u0e01\u0e02\u0e49\u0e32\u0e07\u0e04\u0e39\u0e48\u0e2b\u0e25\u0e31\u0e07"'), 'Rear side windows'),
            'rear_percent' => localized_text_value(json_decode('"\u0e01\u0e23\u0e30\u0e08\u0e01\u0e2b\u0e25\u0e31\u0e07"'), 'Rear windshield'),
        );

        if (!array_key_exists('front_side_percent', $decoded) && !array_key_exists('rear_side_percent', $decoded)) {
            $labels = array(
                'front_percent' => localized_text_value(json_decode('"\u0e01\u0e23\u0e30\u0e08\u0e01\u0e2b\u0e19\u0e49\u0e32"'), 'Front windshield'),
                'side_percent' => localized_text_value(json_decode('"\u0e01\u0e23\u0e30\u0e08\u0e01\u0e14\u0e49\u0e32\u0e19\u0e02\u0e49\u0e32\u0e07"'), 'Side windows'),
                'rear_percent' => localized_text_value(json_decode('"\u0e01\u0e23\u0e30\u0e08\u0e01\u0e2b\u0e25\u0e31\u0e07"'), 'Rear windshield'),
            );
        }

        foreach ($labels as $key => $label) {
            $value = trim((string) ($decoded[$key] ?? ''));

            if ($value === '') {
                continue;
            }

            $displayValue = $value === 'none'
                ? localized_text_value(json_decode('"\u0e44\u0e21\u0e48\u0e23\u0e30\u0e1a\u0e38\u0e40\u0e1b\u0e2d\u0e23\u0e4c\u0e40\u0e0b\u0e47\u0e19\u0e15\u0e4c"'), 'Not specified')
                : $value . '%';

            $lines[] = $label . ': ' . $displayValue;
        }
    }

    $notes = trim((string) ($decoded['notes'] ?? ''));
    if ($notes !== '') {
        $lines[] = localized_text_value(json_decode('"\u0e2b\u0e21\u0e32\u0e22\u0e40\u0e2b\u0e15\u0e38"'), 'Notes') . ': ' . $notes;
    }

    return $lines;
}
