<?php
if (!defined('BASE_URL')) {
    $scheme = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off') ? 'https://' : 'http://';
    $host = $_SERVER['HTTP_HOST'] ?? 'localhost';
    define('BASE_URL', $scheme . $host . '/htdocs/');
}

ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

$isLocal = in_array($_SERVER['HTTP_HOST'] ?? '', ['localhost', '127.0.0.1']);

if ($isLocal) {
    $servername  = "localhost";
    $username_db = "root";
    $password_db = "";
    $dbname      = "if0_39631704_shop"; // ← غيّر هذا لاسم DB في phpMyAdmin
} else {
    $servername  = "sql305.infinityfree.com"; // ← سيرفر InfinityFree
    $username_db = "if0_39631704";
    $password_db = "كلمة_السر_هنا";
    $dbname      = "if0_39631704_shop";
}

try {
    $pdo = new PDO(
        "mysql:host=$servername;dbname=$dbname;charset=utf8mb4",
        $username_db,
        $password_db,
        [
            PDO::ATTR_ERRMODE            => PDO::ERRMODE_EXCEPTION,
            PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
            PDO::ATTR_EMULATE_PREPARES   => false,
        ]
    );
} catch (PDOException $e) {
    die("Database connection failed: " . $e->getMessage());
}

$connect = new mysqli($servername, $username_db, $password_db, $dbname);
$conn    = $connect;
if ($connect->connect_error) {
    die("MySQL connection failed: " . $connect->connect_error);
}
?>