<?php
error_reporting(E_ALL);
ini_set('display_errors', '1');


$content = file_get_contents("php://input");
$update = json_decode($content, true);

include('class/bot.php');
include('class/db.php');
// 7531529065:AAGzkEfsl-VcW-dX-sObJnWkEvXdbgU3xT0
// 8185392686:AAHFTX3Q9Ae0Z5KO5vIx8AGG2vEDt3BBToQ

$bot = new bot("7531529065:AAGzkEfsl-VcW-dX-sObJnWkEvXdbgU3xT0", $content);
$db = new db();


$db->getinfo($bot->chatid, $bot->username, $bot->firstname, $bot->lastname);

$bot->sendmessage(urlencode('here : ' . $bot->message_id), $bot->chatid, $key);


if ($bot->recmsg == "/start" || $bot->recmsg == "🔙 بازگشت") {
    $text = "
🗣 سلام به ربات مجموعه کیوان ملک خوش آمدید
";

    $bot->sendmessage(urlencode($text), $bot->chatid, $bot->mainKey);
    $sql = "update users set  req1='', req2='' where chatid='" . $bot->chatid . "' ";
    $db->conn->exec($sql);

} 

elseif($bot->recmsg == 'خرید موسیقی'){
    $keys = json_encode(
        array(
            "inline_keyboard" => array(
                array(
                    array("text" => urlencode("آلبوم هفت رنگ"), "callback_data" => urlencode("خرید آلبوم")),
                ),
                array(
                    array("text" => urlencode("تک آهنگها"), "callback_data" => urlencode("خرید تک آهنگ")),
                ),
                array(
                    array("text" => urlencode("🔙 بازگشت"), "callback_data" => urlencode("🔙 بازگشت"))
                ),
            )
        )
    );
    $text = "لطفا گزینه ی مورد نظر خود را انتخاب کنید";
    $bot->sendmessage(urlencode($text), $bot->chatid, $keys);

}

elseif($bot->recmsg == "خرید تک آهنگ"){
    
    $keys = json_encode([
        "inline_keyboard" => [
            [
                ["text" => "۱-آهنگ عیان نهان", "callback_data" => "buy_ayannahan_99000"]
            ]
        ]
    ]);

    $res = $bot->copyMessage($bot->chatid, 5803067539, 741, $keys);


    
    $keys = json_encode([
        "inline_keyboard" => [
            [
                ["text" => "۲-آهنگ باتو بی تو", "callback_data" => "buy_batobarto_99000"]
            ]
        ]
    ]);
    $res = $bot->copyMessage($bot->chatid, 5803067539, 743, $keys);



    $keys = json_encode([
        "inline_keyboard" => [
            [
                ["text" => "۳-آهنگ درخدایم", "callback_data" => "buy_darkhodayam_99000"]
            ]
        ]
    ]);
    $res = $bot->copyMessage($bot->chatid, 5803067539, 742, $keys);

    


    $keys = json_encode([
        "inline_keyboard" => [
            [
                ["text" => "۴-آهنگ پرده بدر ", "callback_data" => "buy_pardebadr_99000"]
            ]
        ]
    ]);
    $res = $bot->copyMessage($bot->chatid, 5803067539, 744, $keys);




   
    $keys = json_encode([
        "inline_keyboard" => [
            [
                ["text" => "۵-آهنگ بنام تو", "callback_data" => "buy_benametoo_99000"]
            ]
        ]
    ]);
    $res = $bot->copyMessage($bot->chatid, 5803067539, 745, $keys);




   
    $keys = json_encode([
        "inline_keyboard" => [
            [
                ["text" => "۶-آهنگ خدابین", "callback_data" => "buy_khodabin_99000"]
            ]
        ]
    ]);
    $res = $bot->copyMessage($bot->chatid, 5803067539, 746, $keys);


   
    $keys = json_encode([
        "inline_keyboard" => [
            [
                ["text" => "۷-آهنگ از تو تا تو", "callback_data" => "buy_aztotato_99000"],
                // ["text" => "خرید آهنگ از تو تا تو 💳", "url" => "https://bots.ahpbot.com/dev_test/malek/paying.php?id=$bot->chatid&amount=990000"]
            ],

            [
                ["text" => "🔙 بازگشت", "callback_data" => "🔙 بازگشت"]
            ]
        ]
    ]);

    $res = $bot->copyMessage($bot->chatid, 5803067539, 747, $keys);

    // $text = "فیلم های فرستاده شده دمو هر آهنگ میباشد بر روی خرید آهنگ بر روی دکمه ی خرید زیر هر فیلم کلیک کنید";
    // $bot->sendmessage(urlencode($text), $bot->chatid, $bot->backKey);
}




elseif($bot->recmsg == "خرید آلبوم"){
    $keys = json_encode([
        "inline_keyboard" => [
            [
                ["text" => "آلبوم هفت رنگ", "callback_data" => "buy_kolealbum_499000"],
                // ["text" => "خرید آهنگ از تو تا تو 💳", "url" => "https://bots.ahpbot.com/dev_test/malek/paying.php?id=$bot->chatid&amount=990000"]
            ],

            [
                ["text" => "🔙 بازگشت", "callback_data" => "🔙 بازگشت"]
            ]
        ]
    ]);

    $res = $bot->copyMessage($bot->chatid, 5803067539, 132, $keys);
}

elseif (strpos($bot->recmsg, 'buy') !== false) {

    $exrecmsg = explode("_",$bot->recmsg);
    $song = $exrecmsg[1];
    $price = $exrecmsg[2];

    $text = "در صورتی که کد تخفیف دارید آن را وارد کنید و در غیر این صورت بر روی کد تخفیف ندارم کلیک کنید";


    $keys = urlencode(json_encode([
        "inline_keyboard" => [
            [
                ["text" => "کد تخفیف ندارم", "callback_data" => "nodiscountcode"]
            ],
            [
                ["text" => "🔙 بازگشت", "callback_data" => "🔙 بازگشت"]
            ]
        ]
    ]));

    $bot->sendmessage(urlencode($text), $bot->chatid, $keys);

    $sql = "update users set  req1='pay', req2='$song', req3='$price' where chatid='" . $bot->chatid . "' ";
    $db->conn->exec($sql);

}
elseif ($db->req1 == 'pay') {

    $originalPrice = $db->req3; // قیمت اصلی
    // $discountCode = $bot->recmsg; // کد تخفیف وارد شده
    $discountCode = strtolower($bot->recmsg); // تبدیل کد تخفیف به حروف کوچک
    $finalPrice = $originalPrice; // مقدار پیش‌فرض

    if ($discountCode == 'nodiscountcode') {
        $discountCode = 'none';
    } else {
        $discountCodes = [
            'doostan369' => 0.5,  // 50% تخفیف
            'yaran369' => 1.0     // 100% تخفیف
        ];

        if (array_key_exists($discountCode, $discountCodes)) {
            $discountAmount = $originalPrice * $discountCodes[$discountCode];
            $finalPrice = $originalPrice - $discountAmount;
        } else {
            // کد تخفیف معتبر نیست
            $keys = urlencode(json_encode([
                "inline_keyboard" => [
                    [
                        ["text" => "کد تخفیف ندارم", "callback_data" => "nodiscountcode"]
                    ],
                    [
                        ["text" => "🔙 بازگشت", "callback_data" => "🔙 بازگشت"]
                    ]
                ]
            ]));

            $bot->sendmessage(urlencode("❌ کد تخفیف وارد شده معتبر نیست. لطفاً دوباره امتحان کنید."), $bot->chatid, $keys);
            exit();
        }
    }

    if($finalPrice == 0){

        switch ($db->req2) {
            case "ayannahan":
                $messageId = 145;
                break;
            case "batobarto":
                $messageId = 146;
                break;
            case "darkhodayam":
                $messageId = 147;
                break;
            case "pardebadr":
                $messageId = 148;
                break;
            case "benametoo":
                $messageId = 149;
                break;
            case "khodabin":
                $messageId = 150;
                break;
            case "aztotato":
                $messageId = 151;
                break;
            case "kolealbum":
                $messageId = 386;
                break;
            case "masihian":
                $messageId =345;
                break;
            case "asharat":
                $messageId =346;
                break;
            case "mashlool":
                $messageId =347;
                break;
            case "ghamoos":
                $messageId =348;
                break;
            case "boodayi":
                $messageId =349;
                break;
            case "yahoodian":
                $messageId =350;
                break;
            case "emam":
                $messageId =351;
                break;
            case "sariejabe":
                $messageId =352;
                break;
            case "sabah":
                $messageId =353;
                break;
            case "roozane":
                $messageId =354;
                break;
            case "sorkhpoostan":
                $messageId =355;
                break;
            case "african":
                $messageId =356;
                break;
            case "sahar":
                $messageId =357;
                break;
            case "shafaeslami":
                $messageId =358;
                break;
            case "shafasorkhpoosti":
                $messageId =359;
                break;
            case "shafamasihi":
                $messageId =360;
                break;
            case "shoghl":
                $messageId =384;
                break;
            case "ezdevaj":
                $messageId =361;
                    break; 
            case "yasteshir":
                $messageId =362;
                    break; 
            case "safar":
                $messageId =363;
                    break;                                                                                             
    }


    $res = $bot->copyMessage($bot->chatid, 5803067539, $messageId, $bot->backKey);

    exit();

}

    // دکمه پرداخت با قیمت نهایی
    $keys = urlencode(json_encode([
        "inline_keyboard" => [
            [
                // ["text" => "💳 پرداخت ($finalPrice تومان)", "url" => "https://bots.ahpbot.com/dev_test/malek/paying.php?id=$bot->chatid&amount=$finalPrice&package=$db->req2"]
                ["text" => "💳 پرداخت ($finalPrice تومان)", "url" => "https://bots.ahpbot.com/dev_test/malek/paying.php?id=$bot->chatid&amount=$finalPrice&package=$db->req2"]
            ],
            [
                ["text" => "🔙 بازگشت", "callback_data" => "🔙 بازگشت"]
            ]
        ]
    ]));

    // ارسال پیام پرداخت
    $bot->sendmessage(urlencode("✅ مبلغ نهایی پرداخت: $finalPrice تومان"), $bot->chatid, $keys);
}


elseif($bot->recmsg == "خرید دعا"){

    $keys = json_encode([
        "inline_keyboard" => [
            [
                ["text" => "۱-دعای مسیحیان", "callback_data" => "buy_masihian_69000"]

            ]
        ]
    ]);
    $res = $bot->copyMessage($bot->chatid, 5803067539, 62, $keys);



    $keys = json_encode([
        "inline_keyboard" => [
            [
                ["text" => "۲-دعای عشرات", "callback_data" => "buy_asharat_69000"]

            ]
        ]
    ]);
    $res = $bot->copyMessage($bot->chatid, 5803067539, 63, $keys);




    $keys = json_encode([
        "inline_keyboard" => [
            [
                ["text" => "۳-دعای مشلول", "callback_data" => "buy_mashlool_69000"]

            ]
        ]
    ]);
    $res = $bot->copyMessage($bot->chatid, 5803067539, 64, $keys);




    $keys = json_encode([
        "inline_keyboard" => [
            [
                ["text" => "۴-دعای بودایی", "callback_data" => "buy_boodayi_69000"]

            ]
        ]
    ]);
    $res = $bot->copyMessage($bot->chatid, 5803067539, 65, $keys);




    $keys = json_encode([
        "inline_keyboard" => [
            [
                ["text" => "۵-دعای قاموس", "callback_data" => "buy_ghamoos_69000"]

            ]
        ]
    ]);
    $res = $bot->copyMessage($bot->chatid, 5803067539, 66, $keys);




    $keys = json_encode([
        "inline_keyboard" => [
            [
                ["text" => "۶-دعای یهودیان", "callback_data" => "buy_yahoodian_69000"]

            ]
        ]
    ]);
    $res = $bot->copyMessage($bot->chatid, 5803067539, 67, $keys);





    $keys = json_encode([
        "inline_keyboard" => [
            [
                ["text" => "۷-دعای امام", "callback_data" => "buy_emam_69000"]

            ]
        ]
    ]);
    $res = $bot->copyMessage($bot->chatid, 5803067539, 68, $keys);




    $keys = json_encode([
        "inline_keyboard" => [
            [
                ["text" => "۸-دعای سریع الاجابه", "callback_data" => "buy_sariejabe_69000"]

            ]
        ]
    ]);
    $res = $bot->copyMessage($bot->chatid, 5803067539, 69, $keys);




    $keys = json_encode([
        "inline_keyboard" => [
            [
                ["text" => "۹-دعای صباح", "callback_data" => "buy_sabah_69000"]

            ]
        ]
    ]);
    $res = $bot->copyMessage($bot->chatid, 5803067539, 70, $keys);



    $keys = json_encode([
        "inline_keyboard" => [
            [
                ["text" => "۱۰-دعای روزانه", "callback_data" => "buy_roozane_69000"]

            ]
        ]
    ]);
    $res = $bot->copyMessage($bot->chatid, 5803067539, 71, $keys);



    $keys = json_encode([
        "inline_keyboard" => [
            [
                ["text" => "۱۱-دعای سرخپوستان", "callback_data" => "buy_sorkhpoostan_69000"]

            ]
        ]
    ]);
    $res = $bot->copyMessage($bot->chatid, 5803067539, 72, $keys);

    $keys = json_encode([
        "inline_keyboard" => [
            [
                ["text" => "۱۲-دعای آفریقایی", "callback_data" => "buy_african_69000"]

            ]
        ]
    ]);
    $res = $bot->copyMessage($bot->chatid, 5803067539, 73, $keys);


    $keys = json_encode([
        "inline_keyboard" => [
            [
                ["text" => "۱۳-دعای سحر", "callback_data" => "buy_sahar_69000"]

            ]
        ]
    ]);
    $res = $bot->copyMessage($bot->chatid, 5803067539, 74, $keys);


    $keys = json_encode([
        "inline_keyboard" => [
            [
                ["text" => "۱۴-دعای شفا اسلامی", "callback_data" => "buy_shafaeslami_69000"]

            ]
        ]
    ]);
    $res = $bot->copyMessage($bot->chatid, 5803067539, 75, $keys);

    $keys = json_encode([
        "inline_keyboard" => [
            [
                ["text" => "۱۵-دعای شفا سرخ پوستی", "callback_data" => "buy_shafasorkhpoosti_69000"]

            ]
        ]
    ]);
    $res = $bot->copyMessage($bot->chatid, 5803067539, 76, $keys);


    $keys = json_encode([
        "inline_keyboard" => [
            [
                ["text" => "۱۶-دعای شفا مسیحی", "callback_data" => "buy_shafamasihi_69000"]

            ]
        ]
    ]);
    $res = $bot->copyMessage($bot->chatid, 5803067539, 77, $keys);


    $keys = json_encode([
        "inline_keyboard" => [
            [
                ["text" => "۱۷-دعای شغل", "callback_data" => "buy_shoghl_69000"]

            ]
        ]
    ]);
    $res = $bot->copyMessage($bot->chatid, 5803067539, 78, $keys);


    $keys = json_encode([
        "inline_keyboard" => [
            [
                ["text" => "۱۸-دعای ازدواج", "callback_data" => "buy_ezdevaj_69000"]

            ]
        ]
    ]);
    $res = $bot->copyMessage($bot->chatid, 5803067539, 79, $keys);


    $keys = json_encode([
        "inline_keyboard" => [
            [
                ["text" => "۱۹-دعای یستشیر", "callback_data" => "buy_yasteshir_69000"]

            ]
        ]
    ]);
    $res = $bot->copyMessage($bot->chatid, 5803067539, 80, $keys);

    $keys = json_encode([
        "inline_keyboard" => [
            [
                ["text" => "۲۰-دعای سفر", "callback_data" => "buy_safar_69000"]
            ],

            [
                ["text" => "🔙 بازگشت", "callback_data" => "🔙 بازگشت"]
            ]
        ]
    ]);
    $res = $bot->copyMessage($bot->chatid, 5803067539, 81, $keys);
}