<?php
// Start session if not already started
if (session_status() === PHP_SESSION_NONE) {
    session_start();
}

// Set default password constant (used for resets, login checks, etc.)
define('DEFAULT_PASSWORD', 'Reset123!');

// Database configuration
$servername = "localhost";
$username   = "root"; // Update with your DB username
$password   = "";      // Update with your DB password
$dbname     = "laptrack_digital_pos"; // Update with your DB name

// Set timezone
date_default_timezone_set('Africa/Blantyre');

// Create DB connection
$conn = new mysqli($servername, $username, $password, $dbname);

// Enable MySQLi error reporting for exceptions
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);

// Check connection
if ($conn->connect_error) {
    error_log("DB Connection failed: " . $conn->connect_error);
    echo json_encode(['success' => false, 'message' => 'Database connection failed']);
    exit;
}

// Flash message functions
if (!function_exists('set_flash')) {
    function set_flash($key, $message) {
        if (!isset($_SESSION['flash'])) {
            $_SESSION['flash'] = [];
        }
        $_SESSION['flash'][$key] = $message;
    }
}

if (!function_exists('get_flash')) {
    function get_flash($key) {
        if (!empty($_SESSION['flash'][$key])) {
            $msg = $_SESSION['flash'][$key];
            unset($_SESSION['flash'][$key]);
            return $msg;
        }
        return null;
    }
}
?>
