<?php
function gp_lang(PDO $pdo): string
{
    static $lang = null;
    if ($lang !== null) return $lang;

    $forced = trim((string)($_GET['lang'] ?? ''));
    if (in_array($forced, ['el', 'en'], true)) {
        if (session_status() !== PHP_SESSION_ACTIVE) session_start();
        $_SESSION['gp_lang'] = $forced;
        $lang = $forced;
        return $lang;
    }

    if (session_status() !== PHP_SESSION_ACTIVE) session_start();
    $sessionLang = trim((string)($_SESSION['gp_lang'] ?? ''));
    if (in_array($sessionLang, ['el', 'en'], true)) {
        $lang = $sessionLang;
        return $lang;
    }

    try {
        $stmt = $pdo->prepare("SELECT setting_value FROM settings WHERE setting_key = 'app_language' LIMIT 1");
        $stmt->execute();
        $v = (string)($stmt->fetchColumn() ?: 'el');
        $lang = in_array($v, ['el', 'en'], true) ? $v : 'el';
    } catch (Throwable $e) {
        $lang = 'el';
    }
    return $lang;
}

function gp_tr(PDO $pdo, string $key): string
{
    $lang = gp_lang($pdo);

    $dict = [
        'project' => ['el' => 'Στοιχεία έργου', 'en' => 'Project'],
        'equipment' => ['el' => 'Εξοπλισμός', 'en' => 'Equipment'],
        'payments' => ['el' => 'Πληρωμές', 'en' => 'Payments'],
        'expenses' => ['el' => 'Έξοδα', 'en' => 'Expenses'],
        'tasks' => ['el' => 'Εργασίες', 'en' => 'Tasks'],
        'dispatch' => ['el' => 'Ανάθεση', 'en' => 'Dispatch'],
        'partners' => ['el' => 'Συνεργάτες', 'en' => 'Partners'],
        'map' => ['el' => 'Χάρτης', 'en' => 'Map'],
        'warranties' => ['el' => 'Εγγυήσεις', 'en' => 'Warranties'],
        'send' => ['el' => 'Αποστολή', 'en' => 'Send'],
        'access' => ['el' => 'Πρόσβαση', 'en' => 'Access'],
        'employee_expenses' => ['el' => 'Έξοδα υπαλλήλων', 'en' => 'Employee expenses'],
        'project_statuses' => ['el' => 'Καταστάσεις έργων', 'en' => 'Project statuses'],
        'add_status' => ['el' => 'Προσθήκη κατάστασης', 'en' => 'Add status'],
        'language' => ['el' => 'Γλώσσα', 'en' => 'Language'],
    ];

    return $dict[$key][gp_lang($pdo)] ?? $key;
}

function gp_project_statuses(PDO $pdo, bool $activeOnly = true): array
{
    try {
        $sql = "SELECT status_key, label_el, label_en, is_active FROM project_statuses";
        if ($activeOnly) $sql .= " WHERE is_active = 1";
        $sql .= " ORDER BY sort_order ASC, id ASC";
        $rows = $pdo->query($sql)->fetchAll();

        if ($rows) {
            $lang = gp_lang($pdo);
            $out = [];
            foreach ($rows as $r) {
                $out[(string)$r['status_key']] = $lang === 'en' ? (string)$r['label_en'] : (string)$r['label_el'];
            }
            return $out;
        }
    } catch (Throwable $e) {}

    return [
        'new' => gp_lang($pdo) === 'en' ? 'New' : 'Νέο',
        'in_progress' => gp_lang($pdo) === 'en' ? 'In progress' : 'Σε εξέλιξη',
        'completed' => gp_lang($pdo) === 'en' ? 'Completed' : 'Ολοκληρωμένο',
        'cancelled' => gp_lang($pdo) === 'en' ? 'Cancelled' : 'Ακυρωμένο',
    ];
}

function gp_project_status_label(PDO $pdo, string $status): string
{
    $all = gp_project_statuses($pdo, false);
    return $all[$status] ?? $status;
}
