<?php
define('DB_HOST', 'localhost');
define('DB_NAME', 'violexst_violex');
define('DB_USER', 'violexst_user');
define('DB_PASS', 'YOUR_DB_PASSWORD_HERE');

define('ZIBAL_MERCHANT', '69e3945ee6d570ad00fd0dad');
define('ZIBAL_CALLBACK', 'https://violexq.ir/api/verify.php');
define('ZIBAL_REQUEST_URL', 'https://gateway.zibal.ir/v1/request');
define('ZIBAL_VERIFY_URL', 'https://gateway.zibal.ir/v1/verify');
define('ZIBAL_STARTPAY', 'https://gateway.zibal.ir/start/');

define('API_SECRET_KEY', 'VIOLEX_SECRET_KEY_2026_CHANGE_ME_PLEASE');
define('BOT_API_KEY', 'VIOLEX_BOT_API_KEY_2026_CHANGE_ME_PLEASE');
define('BOT_ACTIVATE_URL', 'https://your-app.up.railway.app/api/activate');
define('BOT_TOKEN', '8762301184:AAGbn9CZirNf7Yc9nRdnBpQMfFGEnu8r9wA');

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 (Exception $e) {
            die(json_encode(['status' => false, 'message' => 'DB Error: ' . $e->getMessage()]));
        }
    }
    return $pdo;
}

function checkApiKey() {
    $headers = getallheaders();
    $api_key = $headers['X-Api-Key'] ?? $headers['X-API-Key'] ?? ($_POST['api_key'] ?? '');
    if ($api_key !== API_SECRET_KEY) {
        http_response_code(403);
        echo json_encode(['status' => false, 'message' => 'Unauthorized']);
        exit;
    }
}

function sendTelegramMessage($chat_id, $text) {
    $url = "https://api.telegram.org/bot" . BOT_TOKEN . "/sendMessage";
    $data = ['chat_id' => $chat_id, 'text' => $text, 'parse_mode' => 'Markdown'];
    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
    curl_setopt($ch, CURLOPT_TIMEOUT, 10);
    $result = curl_exec($ch);
    curl_close($ch);
    return $result;
}
