<?php
// Error reporting strategy
// Define SUPPRESS_ERRORS (true) before including this file in API endpoints to avoid leaking
// HTML notices/warnings that break JSON responses in production.
if (!defined('SUPPRESS_ERRORS') || SUPPRESS_ERRORS === false) {
    // Verbose errors for web pages / local development
    ini_set('display_errors', 1);
    ini_set('display_startup_errors', 1);
    error_reporting(E_ALL);
} else {
    // Suppress display (still log fatal errors) for API JSON responses
    ini_set('display_errors', 0);
    ini_set('display_startup_errors', 0);
    // Reduce noise: keep fatal + critical warnings out of output buffering
    error_reporting(E_ALL & ~E_NOTICE & ~E_WARNING & ~E_DEPRECATED & ~E_STRICT);
}

date_default_timezone_set('Asia/Kolkata');

// Database configuration
$db_host = 'localhost';
$db_user = 'root';  
$db_pass = '';
$db_name = 'uzrs-inventorydb';

// MySQLi connection with better error handling
$con = mysqli_connect($db_host, $db_user, $db_pass, $db_name);
if (mysqli_connect_errno()) {
    error_log('Database connection failed: ' . mysqli_connect_error());
    die('Database connection failed. Please check your database credentials and server status.');
}

mysqli_set_charset($con, 'utf8');

// PDO connection with better error handling
try {
    $dsn = "mysql:host=$db_host;dbname=$db_name;charset=utf8mb4";
    $dbh = new PDO($dsn, $db_user, $db_pass, [
        PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
        PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC
    ]);
} catch (PDOException $e) {
    error_log('PDO connection failed: ' . $e->getMessage());
    $dbh = null;
    // Don't die here, let the application handle the null $dbh
}
?>
