<?php
// session_start();
require("../includes/db_connect.php");
require_once("includes/session_token.php");

$accountId = $_SESSION['usr_accountId'];
$package_name = "";

if (isset($_GET['package'])) {

    $package = $_GET['package'];

    if ($package == "monthly-ninja" || $package == "monthly-subzero" || $package == "monthly-rocket" || $package == "quarterly-rocket" || $package == "quarterly-ninja" || $package == "quarterly-subzero" || $package == "annually-ninja" || $package == "annually-subzero" || $package == "annually-rocket" || $package == "community-bot") {
        $_SESSION['trade_package'] = $package;

        $trade_amount = 0.00;

        switch ($package) {
            case 'monthly-ninja':
                $package_name = "Ninja 2.1 (Monthly)";
                $trade_amount = 1700;
                break;
            case 'monthly-subzero':
                $package_name = "Subzero 3.2 (Monthly)";
                $trade_amount = 3600;
                break;
            case 'monthly-rocket':
                $package_name = "Rocket 4.1 (Monthly)";
                $trade_amount = 7300;
                break;
            case 'quarterly-ninja':
                $package_name = "Ninja 2.1 (Quarterly)";
                $trade_amount = (1300 * 3);
                break;
            case 'quarterly-subzero':
                $package_name = "Subzero 3.2 (Quarterly)";
                $trade_amount = (2800 * 3);
                break;
            case 'quarterly-rocket':
                $package_name = "Rocket 4.1 (Quarterly)";
                $trade_amount = (6250 * 3);
                break;
            case 'annually-ninja':
                $package_name = "Ninja 2.1 (Annually)";
                $trade_amount = (900 * 12);
                break;
            case 'annually-subzero':
                $package_name = "Subzero 3.2 (Annually)";
                $trade_amount = (2000 * 12);
                break;
            case 'annually-rocket':
                $package_name = "Rocket 4.1 (Annually)";
                $trade_amount = (5200 * 12);
                break;
            case 'community-bot':
                $package_name = "Community Bot";
                $trade_amount = 100;
                break;
            default:
                redirect();
                break;
        }

        $balance_sql = "SELECT * FROM wallet WHERE accountId=? LIMIT 1";

        $balance_sth = $conn->prepare($balance_sql, [PDO::ATTR_CURSOR => PDO::CURSOR_FWDONLY]);
        $balance_sth->execute([$accountId]);
        $account_balances = $balance_sth->fetchAll();

        foreach ($account_balances as $key => $value) {
            $usr_balance = $value['balance'];

            $acc_balance = round($usr_balance, 2);

            $new_balance_calc = round(($acc_balance - $trade_amount), 2);

            if ($new_balance_calc >= 0) {
                $balance_upd_sql = "UPDATE wallet SET balance=? WHERE accountId=?";

                $update_balance = $conn->prepare($balance_upd_sql)->execute([$new_balance_calc, $accountId]);

                if ($update_balance) {
                    switch ($package_name) {
                        case 'Ninja 2.1 (Monthly)':
                            $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 = "NJ3.6".time(); 
                            $closed_date = closed_date('monthly');
                            break;
                        case 'Subzero 3.2 (Monthly)':
                            $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 = "SZ5.2".time();
                            $closed_date = closed_date('monthly');
                            break;
                        case 'Rocket 4.1 (Monthly)':
                            $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 = "RT7.1".time();
                            $closed_date = closed_date('monthly');
                            break;
                        case 'Ninja 2.1 (Quarterly)':
                            $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 = "NJ3.6".time();
                            $closed_date = closed_date('quarterly');
                            break;
                        case 'Subzero 3.2 (Quarterly)':
                            $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 = "SZ5.2".time();
                            $closed_date = closed_date('quarterly');
                            break;
                        case 'Rocket 4.1 (Quarterly)':
                            $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 = "RT7.1".time();
                            $closed_date = closed_date('quarterly');
                            break;
                        case 'Ninja 2.1 (Annually)':
                            $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 = "NJ3.6".time();
                            $closed_date = closed_date('annually');
                            break;
                        case 'Subzero 3.2 (Annually)':
                            $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 = "SZ5.2".time();
                            $closed_date = closed_date('annually');
                            break;
                        case 'Rocket 4.1 (Annually)':
                            $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 = "RT7.1".time();
                            $closed_date = closed_date('annually');
                            break;
                        case 'Community Bot':
                            $balance_sql = "SELECT * FROM bot_trading_balances WHERE accountId=? LIMIT 1";
                            $new_balance_sql = "UPDATE bot_trading_balances SET trade_amount=? WHERE accountId=?";
                            $history_account = "bot_trade_history";
                            $history_transactions = "bot_trade_transactions";
                            $history_page = "Location: bot-trading-history";
                            $bot_purchase_id = "CMB".time();
                            $closed_date = closed_date('quarterly');
                            break;
                        default:
                            die("Invalid request!");
                    }


                    $balance_sth = $conn->prepare($balance_sql, [PDO::ATTR_CURSOR => PDO::CURSOR_FWDONLY]);
                    $balance_sth->execute([$accountId]);
                    $account_balances = $balance_sth->fetchAll();

                    foreach ($account_balances as $key => $value) {
                        $usr_balance = $value['trade_amount'];

                        $acc_balance = round($usr_balance, 2);

                        $new_balance = round($acc_balance, 2);

                        $update_balance = $conn->prepare($new_balance_sql)->execute([$new_balance, $accountId]);
                        // $update_balance = true;
                        if ($update_balance) {

                           $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]);
                            header($history_page);
                            exit();
                        } else {
                            die("Unable to update account balance!");
                        }
                    }
                }else {
                    die("An unexpected error occurred! Please try again.");
                }
            } else {
                header('Location: deposit');
                exit();
            }
        }
    } else {
        redirect();
    }
}

function redirect()
{
    header('Location: account');
    exit();
}

function closed_date($type) {
    $currentDate = date('Y-m-d');
    $nextDate = '';

    switch ($type) {
        case 'monthly':
            $nextDate = date('Y-m-d', strtotime($currentDate . ' +31 days'));
            break;
        case 'quarterly':
            $nextDate = date('Y-m-d', strtotime($currentDate . ' +92 days'));
            break;
        case 'annually':
            $nextDate = date('Y-m-d', strtotime($currentDate . ' +366 days'));
            break;
        default:
            return "Invalid type argument.";
    }

    return $nextDate;
}
?>