<?php
/**
 * Telegram Bot Configuration
 * Add these to your .env file
 * 
 * Get bot tokens from: https://t.me/botfather
 * 
 * SETUP INSTRUCTIONS:
 * 1. Create bot with @botfather on Telegram
 * 2. Get the bot token
 * 3. Add token to .env file
 * 4. Set webhook URL: https://your-domain.com/webhooks/telegram.php
 * 5. Test with: curl https://your-domain.com/webhooks/telegram.php?test=1
 */

// Example .env configuration
// Copy these to your .env file and replace with actual values

/*

# ===== TELEGRAM BOT CONFIGURATION =====

# User Bot Token
# Get from @botfather - Create new bot for customer interactions
TELEGRAM_USER_BOT_TOKEN=YOUR_USER_BOT_TOKEN_HERE

# Admin Bot Token  
# Get from @botfather - Create new bot for admin monitoring
TELEGRAM_ADMIN_BOT_TOKEN=YOUR_ADMIN_BOT_TOKEN_HERE

# Admin Chat IDs (comma-separated)
# To get your chat ID: Send /start to @userinfobot
# You can add multiple admin chat IDs here
TELEGRAM_ADMIN_CHAT_IDS=123456789,987654321

# Webhook Configuration
TELEGRAM_WEBHOOK_ENABLED=1

*/

// Helper functions for Telegram bot setup

class TelegramBotSetup {
    
    /**
     * Register webhooks for both bots
     */
    public static function registerWebhooks($userBotToken, $adminBotToken) {
        $domain = getenv('DOMAIN') ?? 'https://boost.kagujje.com';
        
        // User bot webhook
        $userWebhook = $domain . '/webhooks/telegram.php?bot=user';
        $userResult = self::setWebhook($userBotToken, $userWebhook);
        
        // Admin bot webhook
        $adminWebhook = $domain . '/webhooks/telegram.php?bot=admin';
        $adminResult = self::setWebhook($adminBotToken, $adminWebhook);
        
        return [
            'user_bot' => $userResult,
            'admin_bot' => $adminResult
        ];
    }
    
    /**
     * Set webhook for a specific bot
     */
    private static function setWebhook($token, $url) {
        $ch = curl_init();
        curl_setopt_array($ch, [
            CURLOPT_URL => "https://api.telegram.org/bot{$token}/setWebhook",
            CURLOPT_POST => true,
            CURLOPT_POSTFIELDS => ['url' => $url],
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_TIMEOUT => 10
        ]);
        
        $response = curl_exec($ch);
        curl_close($ch);
        
        return json_decode($response, true);
    }
    
    /**
     * Delete webhook (testing locally)
     */
    public static function deleteWebhooks($userBotToken, $adminBotToken) {
        // User bot
        $ch = curl_init();
        curl_setopt_array($ch, [
            CURLOPT_URL => "https://api.telegram.org/bot{$userBotToken}/deleteWebhook",
            CURLOPT_POST => true,
            CURLOPT_RETURNTRANSFER => true,
        ]);
        curl_exec($ch);
        curl_close($ch);
        
        // Admin bot
        $ch = curl_init();
        curl_setopt_array($ch, [
            CURLOPT_URL => "https://api.telegram.org/bot{$adminBotToken}/deleteWebhook",
            CURLOPT_POST => true,
            CURLOPT_RETURNTRANSFER => true,
        ]);
        curl_exec($ch);
        curl_close($ch);
    }
    
    /**
     * Get bot info
     */
    public static function getBotInfo($token) {
        $ch = curl_init();
        curl_setopt_array($ch, [
            CURLOPT_URL => "https://api.telegram.org/bot{$token}/getMe",
            CURLOPT_RETURNTRANSFER => true,
        ]);
        
        $response = curl_exec($ch);
        curl_close($ch);
        
        return json_decode($response, true);
    }
}

// Setup CLI command (run from PHP CLI)
// php telegram-config.php register
// php telegram-config.php info user
// php telegram-config.php info admin

if (php_sapi_name() === 'cli' && isset($argv[1])) {
    $command = $argv[1];
    
    $userToken = getenv('TELEGRAM_USER_BOT_TOKEN');
    $adminToken = getenv('TELEGRAM_ADMIN_BOT_TOKEN');
    
    if ($command === 'register') {
        echo "📝 Registering Telegram webhooks...\n";
        $result = TelegramBotSetup::registerWebhooks($userToken, $adminToken);
        echo json_encode($result, JSON_PRETTY_PRINT) . "\n";
    } elseif ($command === 'delete') {
        echo "🗑️  Deleting Telegram webhooks...\n";
        TelegramBotSetup::deleteWebhooks($userToken, $adminToken);
        echo "Done!\n";
    } elseif ($command === 'info') {
        $bot = $argv[2] ?? 'user';
        $token = $bot === 'admin' ? $adminToken : $userToken;
        
        echo "ℹ️  Getting $bot bot info...\n";
        $info = TelegramBotSetup::getBotInfo($token);
        echo json_encode($info, JSON_PRETTY_PRINT) . "\n";
    } else {
        echo "Usage: php telegram-config.php [command]\n";
        echo "  register  - Register webhooks\n";
        echo "  delete    - Delete webhooks (for testing)\n";
        echo "  info      - Get bot info\n";
    }
}

?>
