<?php
// test_telegram_webhook_manual.php
// Simulasi update Telegram tanpa menunggu Telegram. Buka untuk test insert DB.

declare(strict_types=1);

require_once __DIR__ . '/config/telegram.php';

$payload = [
    'update_id' => time(),
    'message' => [
        'message_id' => random_int(100000, 999999),
        'date' => time(),
        'chat' => [
            'id' => defined('TELEGRAM_CHAT_ID_KEPERAWATAN') ? TELEGRAM_CHAT_ID_KEPERAWATAN : '',
            'title' => 'Keperawatan RS BaliMéd Denpasar',
            'type' => 'supergroup',
        ],
        'from' => [
            'id' => 12345,
            'is_bot' => false,
            'first_name' => 'Test',
            'last_name' => 'Telegram',
            'username' => 'testtelegram',
        ],
        'text' => 'TEST INBOUND MANUAL dari webhook lokal ' . date('Y-m-d H:i:s'),
    ],
];

$ch = curl_init(((!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off') ? 'https' : 'https') . '://' . $_SERVER['HTTP_HOST'] . '/telegram_webhook_chat.php');
curl_setopt_array($ch, [
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_POST => true,
    CURLOPT_HTTPHEADER => ['Content-Type: application/json'],
    CURLOPT_POSTFIELDS => json_encode($payload),
    CURLOPT_TIMEOUT => 20,
]);
$res = curl_exec($ch);
$err = curl_error($ch);
curl_close($ch);

header('Content-Type: text/plain; charset=utf-8');
echo "Webhook response:\n";
echo $res ?: '[empty]';
echo "\n\nCURL error:\n";
echo $err ?: '-';
echo "\n\nSetelah ini buka index.php?module=chat_group dan cek pesan TEST INBOUND MANUAL.\n";
