<?php

// --- Application Configuration ---
const APP_NAME = 'Collections';
const DB_PATH = __DIR__ . '/collections.sqlite';
const DEFAULT_LOCALE = 'nl';
const SUPPORTED_LOCALES = ['nl', 'en'];

// --- Session Start ---
if (session_status() === PHP_SESSION_NONE) {
    session_start();
}

// De autoloader moet geladen zijn voordat we hier komen.
// Gebruik FQCNs om afhankelijkheid van 'use' statements te vermijden.

// --- Twig Setup ---
$loader = new \Twig\Loader\FilesystemLoader(__DIR__ . '/templates');
$twig = new \Twig\Environment($loader, [
    // 'cache' => __DIR__ . '/cache/twig', // Uncomment for production
    'debug' => true,
]);

// --- Translation Setup ---
$translationService = new \App\Services\TranslationService(
    __DIR__ . '/lang',
    SUPPORTED_LOCALES,
    DEFAULT_LOCALE
);

// Get current locale from session or default
$locale = $_SESSION['locale'] ?? DEFAULT_LOCALE;
$translator = $translationService->getTranslator($locale);

// Add translation function to Twig
$twig->addFunction(new \Twig\TwigFunction('trans', function (string $id, array $parameters = [], string $domain = null, string $locale = null) use ($translator) {
    return $translator->trans($id, $parameters, $domain, $locale);
}));

// Add current locale to Twig globals
$twig->addGlobal('current_locale', $locale);
$twig->addGlobal('supported_locales', SUPPORTED_LOCALES);
$twig->addGlobal('app_name', APP_NAME);

// Add translated confirm messages for JS
$twig->addGlobal('delete_part_confirm', $translator->trans('Are you sure you want to delete this part?'));
$twig->addGlobal('delete_category_confirm', $translator->trans('Are you sure you want to delete this category?'));

// Sidebar will be built dynamically via AJAX
$sidebarHtml = '<div id="sidebar-tree">Loading...</div>';
$twig->addGlobal('sidebar_html', $sidebarHtml);
