<?php

$supported_languages = ['pl', 'en'];
$default_language = 'pl';

$translations = [
    'pl' => [
        'login_header' => 'Logowanie',
        'username' => 'Nazwa użytkownika',
        'password' => 'Hasło',
        'login_button' => 'Zaloguj',
        'login_failed' => 'Logowanie nie powiodło się',
        'logout_link' => 'Wyloguj',
        'home_link' => 'Strona główna',
        'cart_link' => 'Koszyk',
        'orders_link' => 'Zamówienia',
        'profile_link' => 'Profil',
        'products_header' => 'Produkty',
        'add_to_cart_button' => 'Dodaj do koszyka',
        'product_details_link' => 'Szczegóły produktu',
        'price_net' => 'Cena netto',
        'price_gross' => 'Cena brutto',
        'net' => 'netto',
        'gross' => 'brutto',
        'unit' => 'jednostka',
        'back_to_products_link' => 'Powrót do listy produktów',
        'product_name' => 'Nazwa produktu',
        'product_description' => 'Opis produktu',
        'related_products_header' => 'Produkty powiązane',
        'cart_header' => 'Koszyk',
        'product' => 'Produkt',
        'quantity' => 'Ilość',
        'total_price_net' => 'Cena całkowita netto',
        'total_price_gross' => 'Cena całkowita brutto',
        'empty_cart_message' => 'Koszyk jest pusty.',
        'update_quantity_button' => 'Aktualizuj',
        'remove_from_cart_button' => 'Usuń',
        'checkout_button' => 'Do kasy',
        'cart_total_header' => 'Podsumowanie koszyka',
        'total_net' => 'Suma netto',
        'total_gross' => 'Suma brutto',
        'checkout_header' => 'Kasa',
        'delivery_address_header' => 'Adres dostawy',
        'name' => 'Imię i nazwisko',
        'street' => 'Ulica',
        'city' => 'Miasto',
        'zip_code' => 'Kod pocztowy',
        'country' => 'Kraj',
        'phone' => 'Telefon',
        'payment_method_header' => 'Metoda płatności',
        'payment_on_delivery' => 'Płatność przy odbiorze',
        'place_order_button' => 'Złóż zamówienie',
        'orders_header' => 'Twoje zamówienia',
        'order_number' => 'Numer zamówienia',
        'order_date' => 'Data zamówienia',
        'total_amount' => 'Kwota całkowita',
        'status' => 'Status',
        'order_details_link' => 'Szczegóły',
        'order_details_header' => 'Szczegóły zamówienia',
        'order_confirmation_header' => 'Potwierdzenie zamówienia',
        'order_confirmation_message' => 'Dziękujemy za złożenie zamówienia. Zostało ono przyjęte do realizacji.',
        'profile_header' => 'Twój profil',
        'email' => 'Email',
        'language_pl' => 'Polski',
        'language_en' => 'English',
        'currency_pln' => 'PLN',
        'currency_eur' => 'EUR',
        'status_pending' => 'Oczekujące',
        'status_pending_payment' => 'Oczekuje na płatność',
        'status_paid' => 'Zapłacone',
        'status_in_progress' => 'W realizacji',
        'status_shipped' => 'Wysłane',
        'status_partially_shipped' => 'Częściowo wysłane',
        'status_completed' => 'Zrealizowane',
        'status_cancelled' => 'Anulowane',
        'payment_bank_transfer' => 'Przelew tradycyjny',
        'payment_online' => 'Płatność online (Przelewy24)',
        'payment_credit' => 'Kredyt kupiecki',
        'payment_trade_credit' => 'Kredyt kupiecki (faktura z odroczonym terminem płatności)',
        'order_confirmation_thank_you' => 'Dziękujemy za złożenie zamówienia.',
        'order_confirmation_order_number' => 'Numer Twojego zamówienia to',
        'order_confirmation_track_status' => 'Możesz śledzić jego status w panelu',
        'order_confirmation_my_orders' => 'Moje zamówienia',
        'order_confirmation_continue_shopping' => 'Kontynuuj zakupy',
        'profile_user_profile' => 'Profil użytkownika',
        'profile_error_loading' => 'Wystąpił błąd podczas ładowania danych profilu. Prosimy spróbować ponownie później.',
        'profile_welcome' => 'Witaj',
        'profile_email_address' => 'Adres e-mail',
        'profile_client' => 'Klient',
        'profile_password_management' => 'Zarządzanie hasłem',
        'profile_feature_in_preparation' => 'Funkcja w przygotowaniu.',
        'db_error' => 'Błąd bazy danych: ',
        'catalog_title' => 'Katalog produktów',
        'no_image_placeholder' => 'Brak zdjęcia',
        'quantity_aria' => 'Ilość',
        'accessories_and_complementary_products' => 'Akcesoria i produkty uzupełniające',
        'footer_text' => 'Wszystkie prawa zastrzeżone.',
        'app_name' => 'B2B Commerce',
        'app_title' => 'B2B Commerce',
        'orders' => 'Zamówienia',
        'order_confirmation' => 'Potwierdzenie zamówienia',
        'thank_you_for_your_order' => 'Dziękujemy za złożenie zamówienia!',
        'your_order_number' => 'Twoje zamówienie o numerze',
        'has_been_placed_successfully' => 'zostało pomyślnie złożone.',
        'order_details_will_be_sent' => 'Szczegóły zamówienia zostaną wkrótce wysłane na Twój adres e-mail. Możesz również sprawdzić status swojego zamówienia w zakładce',
        'continue_shopping' => 'Kontynuuj zakupy',
        'menu_catalog' => 'Katalog',
        'title_cart' => 'Koszyk',
        'menu_orders' => 'Zamówienia',
        'menu_profile' => 'Profil',
        'menu_logout' => 'Wyloguj',
        'header_welcome' => 'Witaj',
        'related_suggestions_title' => 'Dodano do koszyka',
        'product_added_successfully' => 'Produkt został pomyślnie dodany do koszyka!',
        'you_added_to_cart' => 'Dodałeś do koszyka:',
        'related_products_recommendation' => 'Polecamy także produkty powiązane:',
        'go_to_cart' => 'Przejdź do koszyka',
        'back_to_shop' => 'Wróć do sklepu',
        'total' => 'Razem',
        'update_button' => 'Zaktualizuj',
        'remove_button' => 'Usuń',
        'total_gross_label' => 'Razem (brutto):',
        'total_net_label' => 'Razem (netto):',
        'proceed_to_checkout' => 'Przejdź do zamówienia',
        'db_connection_error' => 'Błąd połączenia z bazą danych: ',
        'price_per' => 'Cena za',
        'price_per_unit' => 'Cena za j.m.',
        'db_error_generic' => 'Błąd bazy danych',
        'your_order' => 'Twoje zamówienie',
        'delivery_payment_options' => 'Dostawa i płatność',
        'delivery_source' => 'Źródło dostawy',
        'central_warehouse' => 'Magazyn centralny',
        'external_supplier' => 'Dostawca zewnętrzny',
        'available_trade_credit' => 'Dostępny kredyt kupiecki',
        'order_notes' => 'Uwagi do zamówienia',
        'confirm_order' => 'Potwierdź zamówienie',
        'unauthorized_order_view' => 'Nie masz uprawnień do przeglądania tego zamówienia.',
        'order_not_found_or_unauthorized' => 'Nie znaleziono zamówienia lub nie masz do niego dostępu.',
        'order_details_title' => 'Szczegóły zamówienia nr {order_id}',
        'order_details_fallback_title' => 'Szczegóły zamówienia',
        'back_to_orders' => 'Powrót do listy zamówień',
        'summary' => 'Podsumowanie',
        'notes' => 'Uwagi',
        'order_details' => 'Szczegóły zamówienia',
        'image' => 'Zdjęcie',
        'unit_price_net' => 'Cena jedn. netto',
        'unit_price_gross' => 'Cena jedn. brutto',
        'back_to_product_list' => 'Powrót do listy produktów',
        'product_thumbnail_alt' => 'Miniatura produktu',
        'quantity_label' => 'Ilość',
        'btn_add_to_cart' => 'Dodaj do koszyka',
        'description_tab' => 'Opis',
        'technical_data_tab' => 'Dane techniczne',
        'documents_tab' => 'Dokumenty',
        'related_products_tab' => 'Produkty powiązane',
        'no_technical_data' => 'Brak danych technicznych.',
        'no_documents' => 'Brak dokumentów.',
        'no_related_products' => 'Brak produktów powiązanych.',
        'error_client_id_not_found' => 'Nie znaleziono ID klienta w sesji.',
        'error_fetching_orders' => 'Błąd podczas pobierania zamówień.',
        'title_orders' => 'Zamówienia',
        'order_history' => 'Historia zamówień',
        'no_orders_yet' => 'Nie złożyłeś jeszcze żadnych zamówień.',
        'btn_view_details' => 'Zobacz szczegóły',
        'my_order_items' => 'Moje pozycje zamówień',
        'order_id' => 'ID Zamówienia',
        'last_updated' => 'Ostatnia aktualizacja',
        'action' => 'Akcja',
        'no_order_items_found' => 'Nie znaleziono żadnych pozycji zamówień.',
        'save' => 'Zapisz',
        'login_error' => 'Nieprawidłowy email lub hasło.',
        'payment_method' => 'Metoda płatności',
        'checkout_title' => 'Zamówienie',
        'szt' => 'sztuka',
        'mb' => 'metr bieżący',
        'm2' => 'metr kwadratowy',
        'rolka' => 'rolka',
        'login_email' => 'Adres e-mail',
        'login_password' => 'Hasło',
        'credit_limit' => 'Limit kredytowy',
        'credit_balance' => 'Saldo kredytowe',
        'order_summary' => 'Podsumowanie zamówienia',
        'grand_total' => 'Suma całkowita',
        'price' => 'Cena',
        'chat_with_assistant' => 'Czat z naszym asystentem',
        'chat_hello' => 'Cześć! W czym mogę dzisiaj pomóc?',
        'chat_placeholder' => 'Wpisz swoją wiadomość...',
        'chat_send_button' => 'Wyślij',
        'change_password' => 'Zmiana hasła',
        'current_password' => 'Aktualne hasło',
        'new_password' => 'Nowe hasło',
        'confirm_new_password' => 'Potwierdź nowe hasło',
        'change_password_btn' => 'Zmień hasło',
        'password_changed_successfully' => 'Hasło zostało zmienione pomyślnie.',
        'passwords_do_not_match' => 'Nowe hasła nie są zgodne.',
        'incorrect_current_password' => 'Nieprawidłowe aktualne hasło.',
        'download_pdf' => 'Pobierz PDF',
        'pdf_order_details_title' => 'Szczegóły zamówienia',
        'pdf_order_id' => 'Nr zamówienia',
        'pdf_client' => 'Klient',
        'pdf_email' => 'Email',
        'pdf_status' => 'Status',
        'pdf_total' => 'Suma',
        'pdf_created_at' => 'Data złożenia',
        'pdf_product' => 'Produkt',
        'pdf_quantity' => 'Ilość',
        'pdf_price' => 'Cena',
        'additional_materials_tab' => 'Wytyczne projektowe i dla wykonawców',
        'no_additional_materials' => 'Brak dodatkowych materiałów.',
        'invalid_youtube_url' => 'Nieprawidłowy adres URL YouTube.',
    ],
    'en' => [
        'login_header' => 'Login',
        'username' => 'Username',
        'password' => 'Password',
        'credit_limit' => 'Credit limit',
        'credit_balance' => 'Credit balance',
        'login_button' => 'Login',
        'login_failed' => 'Login failed',
        'logout_link' => 'Logout',
        'home_link' => 'Home',
        'cart_link' => 'Cart',
        'orders_link' => 'Orders',
        'profile_link' => 'Profile',
        'products_header' => 'Products',
        'add_to_cart_button' => 'Add to cart',
        'product_details_link' => 'Product details',
        'price_net' => 'Price excl. VAT',
        'price_gross' => 'Price incl. VAT',
        'net' => 'excl. VAT',
        'gross' => 'incl. VAT',
        'unit' => 'unit',
        'back_to_products_link' => 'Back to product list',
        'product_name' => 'Product name',
        'product_description' => 'Product description',
        'related_products_header' => 'Related products',
        'cart_header' => 'Cart',
        'product' => 'Product',
        'quantity' => 'Quantity',
        'total_price_net' => 'Total price net',
        'total_price_gross' => 'Total price gross',
        'empty_cart_message' => 'Your cart is empty.',
        'update_quantity_button' => 'Update',
        'remove_from_cart_button' => 'Remove',
        'checkout_button' => 'Checkout',
        'cart_total_header' => 'Cart summary',
        'total_net' => 'Total net',
        'total_gross' => 'Total gross',
        'checkout_header' => 'Checkout',
        'delivery_address_header' => 'Delivery address',
        'name' => 'Full name',
        'street' => 'Street',
        'city' => 'City',
        'zip_code' => 'Zip code',
        'country' => 'Country',
        'phone' => 'Phone',
        'payment_method_header' => 'Payment method',
        'payment_on_delivery' => 'Payment on delivery',
        'place_order_button' => 'Place order',
        'orders_header' => 'Your orders',
        'order_number' => 'Order number',
        'order_date' => 'Order date',
        'total_amount' => 'Total amount',
        'status' => 'Status',
        'order_details_link' => 'Details',
        'order_details_header' => 'Order details',
        'order_confirmation_header' => 'Order confirmation',
        'order_confirmation_message' => 'Thank you for your order. It has been accepted for processing.',
        'profile_header' => 'Your profile',
        'email' => 'Email',
        'language_pl' => 'Polski',
        'language_en' => 'English',
        'currency_pln' => 'PLN',
        'currency_eur' => 'EUR',
        'status_pending' => 'Pending',
        'status_pending_payment' => 'Pending payment',
        'status_paid' => 'Paid',
        'status_in_progress' => 'In progress',
        'status_shipped' => 'Shipped',
        'status_partially_shipped' => 'Partially shipped',
        'status_completed' => 'Completed',
        'status_cancelled' => 'Cancelled',
        'payment_bank_transfer' => 'Bank transfer',
        'payment_online' => 'Online payment (Przelewy24)',
        'payment_credit' => 'Trade credit',
        'payment_trade_credit' => 'Trade credit (invoice with deferred payment)',
        'order_confirmation_thank_you' => 'Thank you for your order.',
        'order_confirmation_order_number' => 'Your order number is',
        'order_confirmation_track_status' => 'You can track its status in the',
        'order_confirmation_my_orders' => 'My orders',
        'order_confirmation_continue_shopping' => 'Continue shopping',
        'profile_user_profile' => 'User profile',
        'profile_error_loading' => 'There was an error loading your profile data. Please try again later.',
        'profile_welcome' => 'Welcome',
        'profile_email_address' => 'Email address',
        'profile_client' => 'Client',
        'profile_password_management' => 'Password management',
        'profile_feature_in_preparation' => 'Feature in preparation.',
        'db_error' => 'Database error: ',
        'catalog_title' => 'Product catalog',
        'no_image_placeholder' => 'No image',
        'quantity_aria' => 'Quantity',
        'accessories_and_complementary_products' => 'Accessories and complementary products',
        'footer_text' => 'All rights reserved.',
        'app_name' => 'B2B Commerce',
        'app_title' => 'B2B Commerce',
        'orders' => 'Orders',
        'order_confirmation' => 'Order confirmation',
        'thank_you_for_your_order' => 'Thank you for your order!',
        'your_order_number' => 'Your order number',
        'has_been_placed_successfully' => 'has been placed successfully.',
        'order_details_will_be_sent' => 'Order details will be sent to your email address soon. You can also check your order status in the',
        'continue_shopping' => 'Continue shopping',
        'menu_catalog' => 'Catalog',
        'title_cart' => 'Cart',
        'menu_orders' => 'Orders',
        'menu_profile' => 'Profile',
        'menu_logout' => 'Logout',
        'header_welcome' => 'Welcome',
        'related_suggestions_title' => 'Added to cart',
        'product_added_successfully' => 'Product has been successfully added to the cart!',
        'you_added_to_cart' => 'You added to cart:',
        'related_products_recommendation' => 'We also recommend related products:',
        'go_to_cart' => 'Go to cart',
        'back_to_shop' => 'Back to shop',
        'total' => 'Total',
        'update_button' => 'Update',
        'remove_button' => 'Remove',
        'total_gross_label' => 'Total (gross):',
        'total_net_label' => 'Total (net):',
        'proceed_to_checkout' => 'Proceed to checkout',
        'db_connection_error' => 'Database connection error: ',
        'price_per' => 'Price per',
        'price_per_unit' => 'Price per unit',
        'db_error_generic' => 'Database error',
        'your_order' => 'Your order',
        'delivery_payment_options' => 'Delivery and payment',
        'delivery_source' => 'Delivery source',
        'central_warehouse' => 'Central warehouse',
        'external_supplier' => 'External supplier',
        'available_trade_credit' => 'Available trade credit',
        'order_notes' => 'Order notes',
        'confirm_order' => 'Confirm order',
        'unauthorized_order_view' => 'You are not authorized to view this order.',
        'order_not_found_or_unauthorized' => 'Order not found or you are not authorized to view it.',
        'order_details_title' => 'Order details {order_id}',
        'order_details_fallback_title' => 'Order details',
        'back_to_orders' => 'Back to orders',
        'summary' => 'Summary',
        'notes' => 'Notes',
        'order_details' => 'Order details',
        'image' => 'Image',
        'unit_price_net' => 'Unit price net',
        'unit_price_gross' => 'Unit price gross',
        'back_to_product_list' => 'Back to product list',
        'product_thumbnail_alt' => 'Product thumbnail',
        'quantity_label' => 'Quantity',
        'btn_add_to_cart' => 'Add to cart',
        'description_tab' => 'Description',
        'technical_data_tab' => 'Technical data',
        'documents_tab' => 'Documents',
        'related_products_tab' => 'Related products',
        'no_technical_data' => 'No technical data available.',
        'no_documents' => 'No documents available.',
        'no_related_products' => 'No related products available.',
        'error_client_id_not_found' => 'Client ID not found in session.',
        'error_fetching_orders' => 'Error fetching orders.',
        'title_orders' => 'Orders',
        'order_history' => 'Order history',
        'no_orders_yet' => 'You have no orders yet.',
        'btn_view_details' => 'View details',
        'my_order_items' => 'My order items',
        'order_id' => 'Order ID',
        'last_updated' => 'Last updated',
        'action' => 'Action',
        'no_order_items_found' => 'No order items found.',
        'save' => 'Save',
        'login_error' => 'Invalid email or password.',
        'payment_method' => 'Payment method',
        'checkout_title' => 'Checkout',
        'szt' => 'piece',
        'mb' => 'linear meter',
        'm2' => 'square meter',
        'rolka' => 'roll',
        'login_email' => 'Email',
        'login_password' => 'Password',
        'order_summary' => 'Order Summary',
        'grand_total' => 'Grand Total',
        'price' => 'Price',
        'chat_with_assistant' => 'Chat with our Assistant',
        'chat_hello' => 'Hello! How can I help you today?',
        'chat_placeholder' => 'Type your message...',
        'chat_send_button' => 'Send',
        'change_password' => 'Change Password',
        'current_password' => 'Current Password',
        'new_password' => 'New Password',
        'confirm_new_password' => 'Confirm New Password',
        'change_password_btn' => 'Change Password',
        'password_changed_successfully' => 'Password changed successfully.',
        'passwords_do_not_match' => 'New passwords do not match.',
        'incorrect_current_password' => 'Incorrect current password.',
        'download_pdf' => 'Download PDF',
        'pdf_order_details_title' => 'Order Details',
        'pdf_order_id' => 'Order ID',
        'pdf_client' => 'Client',
        'pdf_email' => 'Email',
        'pdf_status' => 'Status',
        'pdf_total' => 'Total',
        'pdf_created_at' => 'Creation date',
        'pdf_product' => 'Product',
        'pdf_quantity' => 'Quantity',
        'pdf_price' => 'Price',
        'additional_materials_tab' => 'Additional Materials',
        'no_additional_materials' => 'No additional materials available.',
        'invalid_youtube_url' => 'Invalid YouTube URL.',
    ],
];

function get_primary_language() {
    global $supported_languages;
    return $supported_languages[0] ?? 'en';
}

function get_lang() {
    global $default_language;
    return $_SESSION['lang'] ?? $default_language;
}

function set_lang($lang) {
    global $supported_languages;
    if (in_array($lang, $supported_languages)) {
        $_SESSION['lang'] = $lang;
    }
}

function t(string $key): string {
    global $translations;
    $lang = get_lang();
    if (isset($translations[$lang][$key])) {
        return $translations[$lang][$key];
    }
    $primary_lang = get_primary_language();
    if (isset($translations[$primary_lang][$key])) {
        return $translations[$primary_lang][$key];
    }
    return "[missing:$key]";
}

function t_status($key) {
    $payment_methods = ['bank_transfer', 'online', 'credit'];
    if (in_array($key, $payment_methods)) {
        $translation_key = 'payment_' . $key;
    } else {
        $translation_key = 'status_' . $key;
    }
    return t($translation_key);
}