<?php
session_start();

require("../includes/db_connect.php");


if ($_SERVER['REQUEST_METHOD'] === 'POST') {

    if ((!empty($_POST['accountId'])) && (!empty($_POST['bot']))  && (!empty($_POST['botType']))) {

        if ($conn) {
            try {

                $accountId = $_POST['accountId'];
                $bot = $_POST['bot'];
                $botType = $_POST['botType'];

                $package_name = $botType." (".$bot.")";
                $trade_amount = 0.00;

                switch ($botType) {
                    case 'Ninja 2.1':
                        switch ($bot) {
                            case 'Monthly':
                                $trade_amount = 1700;
                                $balance_sql = "SELECT * FROM monthly_trading_balances WHERE accountId=? LIMIT 1";
                                $new_balance_sql = "UPDATE monthly_trading_balances SET trade_amount=? WHERE accountId=?";
                                $history_account = "monthly_trade_history";
                                $history_transactions = "monthly_trade_transactions";
                                $history_page = "Location: monthly-trading-history";
                                $bot_purchase_id = "NJ2.1".time();
                                break;
                            case 'Quarterly':
                                $trade_amount = 1300;
                                $balance_sql = "SELECT * FROM quarterly_trading_balances WHERE accountId=? LIMIT 1";
                                $new_balance_sql = "UPDATE quarterly_trading_balances SET trade_amount=? WHERE accountId=?";
                                $history_account = "quarterly_trade_history";
                                $history_transactions = "quarterly_trade_transactions";
                                $history_page = "Location: quarterly-trading-history";
                                $bot_purchase_id = "NJ2.1".time();
                                break;
                            case 'Annually':
                                $trade_amount = 18000;
                                $balance_sql = "SELECT * FROM annual_trading_balances WHERE accountId=? LIMIT 1";
                                $new_balance_sql = "UPDATE annual_trading_balances SET trade_amount=? WHERE accountId=?";
                                $history_account = "annual_trade_history";
                                $history_transactions = "annual_trade_transactions";
                                $history_page = "Location: annually-trading-history";
                                $bot_purchase_id = "NJ2.1".time();
                                break;
                            default:
                                break;
                        }
                        break;
                    case 'Subzero 3.2':
                        switch ($bot) {
                            case 'Monthly':
                                $trade_amount = 3600;
                                $balance_sql = "SELECT * FROM monthly_trading_balances WHERE accountId=? LIMIT 1";
                                $new_balance_sql = "UPDATE monthly_trading_balances SET trade_amount=? WHERE accountId=?";
                                $history_account = "monthly_trade_history";
                                $history_transactions = "monthly_trade_transactions";
                                $history_page = "Location: monthly-trading-history";
                                $bot_purchase_id = "SZ3.2".time();
                                break;
                            case 'Quarterly':
                                $trade_amount = 2800;
                                $balance_sql = "SELECT * FROM quarterly_trading_balances WHERE accountId=? LIMIT 1";
                                $new_balance_sql = "UPDATE quarterly_trading_balances SET trade_amount=? WHERE accountId=?";
                                $history_account = "quarterly_trade_history";
                                $history_transactions = "quarterly_trade_transactions";
                                $history_page = "Location: quarterly-trading-history";
                                $bot_purchase_id = "SZ3.2".time();
                                break;
                            case 'Annually':
                                $trade_amount = 38400;
                                $balance_sql = "SELECT * FROM annual_trading_balances WHERE accountId=? LIMIT 1";
                                $new_balance_sql = "UPDATE annual_trading_balances SET trade_amount=? WHERE accountId=?";
                                $history_account = "annual_trade_history";
                                $history_transactions = "annual_trade_transactions";
                                $history_page = "Location: annually-trading-history";
                                $bot_purchase_id = "SZ3.2".time();
                                break;
                            default:
                                break;
                        }
                        break;
                    case 'Rocket 4.1':
                        switch ($bot) {
                            case 'Monthly':
                                $trade_amount = 7300;
                                $balance_sql = "SELECT * FROM monthly_trading_balances WHERE accountId=? LIMIT 1";
                                $new_balance_sql = "UPDATE monthly_trading_balances SET trade_amount=? WHERE accountId=?";
                                $history_account = "monthly_trade_history";
                                $history_transactions = "monthly_trade_transactions";
                                $history_page = "Location: monthly-trading-history";
                                $bot_purchase_id = "RT4.1".time();
                                break;
                            case 'Quarterly':
                                $trade_amount = 6250;
                                $balance_sql = "SELECT * FROM quarterly_trading_balances WHERE accountId=? LIMIT 1";
                                $new_balance_sql = "UPDATE quarterly_trading_balances SET trade_amount=? WHERE accountId=?";
                                $history_account = "quarterly_trade_history";
                                $history_transactions = "quarterly_trade_transactions";
                                $history_page = "Location: quarterly-trading-history";
                                $bot_purchase_id = "RT4.1".time();
                                break;
                            case 'Annually':
                                $trade_amount = 80400;
                                $balance_sql = "SELECT * FROM annual_trading_balances WHERE accountId=? LIMIT 1";
                                $new_balance_sql = "UPDATE annual_trading_balances SET trade_amount=? WHERE accountId=?";
                                $history_account = "annual_trade_history";
                                $history_transactions = "annual_trade_transactions";
                                $history_page = "Location: annually-trading-history";
                                $bot_purchase_id = "RT4.1".time();
                                break;
                            default:
                                break;
                        }
                        break;
                    default:
                        die('Invalid request.');
                }

                $inv_sql = "INSERT INTO purchased_bots (accountId, type, status, bot_purchase_id) VALUES (?,?,?,?)";
                $create_inv = $conn->prepare($inv_sql)->execute([$accountId, $package_name, 1, $bot_purchase_id]);
                

                $req_sql = "INSERT INTO transaction_history (accountId, name, amount, type, status) VALUES (?,?,?,?,?)";

                $trans_name = "Bot Purchase";

                $create_history = $conn->prepare($req_sql)->execute([$accountId, $package_name, $trade_amount, $trans_name, 1]);
                
                die("success");
                
            } catch (PDOException $e) {
                die('An unexpected error occurred. Connection failed');
            }
        } else {
            die('An unexpected error occurred. Connection failed.');
        }
    } else {
         die('Invalid user information.');
    }
} else {
    die('Invalid request.');
}