<?php
header('Content-Type: application/json');
if ($_SERVER['REQUEST_METHOD'] !== 'POST') { http_response_code(405); exit; }

require_once 'config/database.php';
require_once 'config/settings.php';
require_once 'config/telegram.php';
require_once 'includes/auth.php';
require_once 'includes/functions.php';

$update = json_decode(file_get_contents('php://input'), true);
if (!$update) { http_response_code(400); exit; }

try {
    $bot = new TelegramBot();
    if ($bot->isConfigured()) $bot->handleWebhook($update);
} catch (Exception $e) { error_log("TG Error: " . $e->getMessage()); }

http_response_code(200);
echo json_encode(['ok' => true]);
