<?php
// Generated by setup_mariadb_project.sh — edit as needed.
define('DB_HOST', '127.0.0.1');
define('DB_NAME', 'app_38283');
define('DB_USER', 'app_38283');
define('DB_PASS', 'c618c07e-42e7-4598-8425-9d48d0b03db3');

function db() {
  static $pdo;
  if (!$pdo) {
    $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,
    ]);
  }
  return $pdo;
}

/**
 * Send message to Telegram Bot
 */
function sendTelegramMessage($message) {
    $db = db();
    try {
        $stmt = $db->query("SELECT key_name, key_value FROM settings WHERE key_name IN ('tg_bot_token', 'tg_chat_id')");
        $settings = $stmt->fetchAll(PDO::FETCH_KEY_PAIR);
        
        $token = $settings['tg_bot_token'] ?? '';
        $chat_id = $settings['tg_chat_id'] ?? '';
        
        if (empty($token) || empty($chat_id)) {
            return false;
        }
        
        $url = "https://api.telegram.org/bot{$token}/sendMessage";
        $data = [
            'chat_id' => $chat_id,
            'text' => $message,
            'parse_mode' => 'Markdown'
        ];
        
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_TIMEOUT, 10);
        $response = curl_exec($ch);
        curl_close($ch);
        
        return $response;
    } catch (Exception $e) {
        return false;
    }
}