<?php
// ─── Database Configuration ────────────────────────────────────────────────
define('DB_HOST', 'localhost');
define('DB_USER', 'root');
define('DB_PASS', '');
define('DB_NAME', 'balimeen_steel');

// ─── Site Configuration ────────────────────────────────────────────────────
define('SITE_NAME', 'Balimeen Private Limited');
define('SITE_TAGLINE', 'Premium Stainless Steel Kitchen Sinks');
define('SITE_URL', 'http://localhost/bm/steel');
define('SITE_EMAIL', 'info@balimeen.com');
define('SITE_PHONE', '+91 7870626467');
define('SITE_PHONE2', '+91 9661427644');
define('SITE_ADDRESS', 'Plot No-1695, Darzi Bigha, Umga, Madanpur, Aurangabad (Bihar) 824208');
define('SITE_GSTIN', '10AAOCB2122R1ZO');
define('UPLOADS_DIR', __DIR__ . '/uploads/');
define('UPLOADS_URL', SITE_URL . '/uploads/');

// ─── Database Connection ────────────────────────────────────────────────────
function getDB() {
    static $pdo = null;
    if ($pdo === null) {
        try {
            $pdo = new PDO(
                "mysql:host=" . DB_HOST . ";dbname=" . DB_NAME . ";charset=utf8mb4",
                DB_USER, DB_PASS,
                [PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
                 PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
                 PDO::ATTR_EMULATE_PREPARES => false]
            );
        } catch (PDOException $e) {
            die(json_encode(['error' => 'DB connection failed: ' . $e->getMessage()]));
        }
    }
    return $pdo;
}

// ─── Helpers ───────────────────────────────────────────────────────────────
function h($s) { return htmlspecialchars($s ?? '', ENT_QUOTES, 'UTF-8'); }
function redirect($url) { header("Location: $url"); exit; }
function isAdmin()    { return isset($_SESSION['admin_logged_in']) && $_SESSION['admin_logged_in'] === true; }
function requireAdmin() { if (!isAdmin()) { redirect(SITE_URL . '/admin/login.php'); } }
function isCustomer() { return isset($_SESSION['customer_id']) && $_SESSION['customer_id'] > 0; }
function requireCustomer() { if (!isCustomer()) { redirect(SITE_URL . '/pages/login.php?next=' . urlencode($_SERVER['REQUEST_URI'])); } }
function currentCustomer() { return ['id'=>$_SESSION['customer_id']??0,'name'=>$_SESSION['customer_name']??'','email'=>$_SESSION['customer_email']??'']; }
function slugify($text) {
    $text = strtolower(trim($text));
    $text = preg_replace('/[^a-z0-9\s-]/', '', $text);
    $text = preg_replace('/[\s-]+/', '-', $text);
    return trim($text, '-');
}
function rupee($n) { return '₹' . number_format((float)$n, 2); }

// ─── Cart helpers (session-based) ──────────────────────────────────────────
function cartAdd($product_id, $qty = 1) {
    if (!isset($_SESSION['cart'])) $_SESSION['cart'] = [];
    $id = (int)$product_id;
    $_SESSION['cart'][$id] = ($_SESSION['cart'][$id] ?? 0) + (int)$qty;
}
function cartRemove($product_id) { unset($_SESSION['cart'][(int)$product_id]); }
function cartUpdate($product_id, $qty) {
    $qty = (int)$qty;
    if ($qty <= 0) { cartRemove($product_id); return; }
    $_SESSION['cart'][(int)$product_id] = $qty;
}
function cartCount() { return array_sum($_SESSION['cart'] ?? []); }
function cartClear()  { $_SESSION['cart'] = []; }
function cartItems()  {
    $cart = $_SESSION['cart'] ?? [];
    if (!$cart) return [];
    $db   = getDB();
    $ids  = implode(',', array_map('intval', array_keys($cart)));
    $rows = $db->query("SELECT * FROM products WHERE id IN ($ids) AND status='active'")->fetchAll();
    foreach ($rows as &$r) $r['qty'] = $cart[$r['id']] ?? 1;
    return $rows;
}
function cartTotal() {
    $total = 0;
    foreach (cartItems() as $item) $total += $item['price'] * $item['qty'];
    return $total;
}

session_start();
