<?php
if (session_status() === PHP_SESSION_NONE) {
    session_start();
}

// Force PHP timezone to Nairobi
date_default_timezone_set('Africa/Nairobi');

// ----------------------------------------------------
// Environment Configuration
// ----------------------------------------------------

// Define ENVIRONMENT constant
if (!defined('ENVIRONMENT')) {
    define('ENVIRONMENT', 'development'); // Change to 'production' on live server
}

// ----------------------------------------------------
// Database Configuration
// ----------------------------------------------------
$host = 'localhost';
 $db = 'test';
//$db = 'webforge';
//creati15_webforge
$user = 'root';
$pass = '29TAc5is1p?2025&!';

$dsn = "mysql:host=$host;dbname=$db;charset=utf8";
$username = $user;
$password = $pass;

try {
    $pdo = new PDO($dsn, $username, $password);
    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
    die("Could not connect to the database: " . $e->getMessage());
}

// ----------------------------------------------------
// Optional Database Helper Class (for B2C, API calls)
// ----------------------------------------------------
if (!class_exists('Database')) {
    class Database {
        private $pdo;

        public function __construct($pdo) {
            $this->pdo = $pdo;
        }

        public function fetchAll($sql, $params = []) {
            $stmt = $this->pdo->prepare($sql);
            $stmt->execute($params);
            return $stmt->fetchAll(PDO::FETCH_ASSOC);
        }

        public function fetchOne($sql, $params = []) {
            $stmt = $this->pdo->prepare($sql);
            $stmt->execute($params);
            return $stmt->fetch(PDO::FETCH_ASSOC);
        }

        public function execute($sql, $params = []) {
            $stmt = $this->pdo->prepare($sql);
            return $stmt->execute($params);
        }
    }
}
?>