<?php
// src/auth.php
require_once __DIR__ . '/../config/db.php';

function is_logged_in(){ return !empty($_SESSION['user_id']); }
function require_login(){
    if(!is_logged_in()){
        header('Location: login.php'); exit;
    }
}
function current_user(){
    if(!is_logged_in()) return null;
    return ['id'=>$_SESSION['user_id'], 'username'=>$_SESSION['username'],'display_name'=>$_SESSION['display_name'] ?? null];
}

// language switch (ar/en)
if(isset($_GET['lang'])){
    $lang = $_GET['lang']=='en' ? 'en':'ar';
    $_SESSION['lang'] = $lang;
}
$lang = $_SESSION['lang'] ?? 'ar';

// simple translations (extend as needed)
$T = [
  'ar' => [
    'welcome'=>'مرحباً',
    'logout'=>'تسجيل الخروج',
    'products'=>'المنتجات',
    'customers'=>'العملاء',
    'sales'=>'المبيعات',
    'backup'=>'نسخة احتياطية'
  ],
  'en' => [
    'welcome'=>'Welcome',
    'logout'=>'Logout',
    'products'=>'Products',
    'customers'=>'Customers',
    'sales'=>'Sales',
    'backup'=>'Backup'
  ]
];
function t($key){
    global $T, $lang;
    return $T[$lang][$key] ?? $key;
}
