<?php
session_start();

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

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

    if ($_POST['redeem_code'] != "" && $_POST['create_amount'] != "" && $_POST['create_account_type'] != "" && $_POST['currency_type'] != "") {

        $redeem_code = $_POST['redeem_code'];
        $create_amount = $_POST['create_amount'];
        $create_account_type = $_POST['create_account_type'];
        $currency_type = $_POST['currency_type'];
        $status = "created";
        
        if ($currency_type != "USD" && $currency_type != "ETH" && $currency_type != "BTC") {
            die('Invalid currency!');
        }

        if (is_numeric($create_amount) && $create_amount >= 0){
            $req_sql = "INSERT INTO redeem_code (redeem_code, account_id, account_type, amount, currency, status,date) VALUES (?,?,?,?,?,?,?)";
            $create_redeem = $conn->prepare($req_sql)->execute([$redeem_code, '', $create_account_type, $create_amount, $currency_type, $status, date("Y-m-d H:i:s")]);
            if ($create_redeem) {
                die("success");
            } else {
                die("An unexpected error occurred. Please try again.");
            }
        } else {
            die('Please enter valid amounts. Avoid commas and negative value inputs.');
        }
    } else {
        die('Invalid user information.');
    }
} else {
    die('Invalid request.');
}
