<?php
session_start();

use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;
use Codenixsv\CoinGeckoApi\CoinGeckoClient;


require_once '../include/Constants.php';
require_once "../vendor/autoload.php";
require_once "../vendor1/autoload.php";
require_once "../include/DatabaseConn.php";
require_once "../models/PaymentWallet.php";
require_once "../models/User.php";
require_once '../models/USettings.php';
require_once '../models/Settings.php';
require_once '../models/Invoice.php';
require_once '../include/GoogleAuthenticator.php';
require_once '../models/News.php';
require_once '../include/Helper.php';

$db = new DatabaseConn();
$link = $db->connect();


$ee = filter_input(INPUT_POST, "email", FILTER_VALIDATE_EMAIL);

$user = User::getCurrentUser($link, $ee);

if (is_null($user)) {
    $url = $_SERVER['REQUEST_URI'];
    header("location: ../login?" . RETURN_URL . "=" . $url);
    die();
}

if (isset($_POST["coin"])) {
    $amount = filter_input(INPUT_POST, "amount", FILTER_VALIDATE_FLOAT);
    $coin = filter_input(INPUT_POST, "coin", FILTER_SANITIZE_STRING);
    if (is_null($amount) || is_bool($amount) || $amount < 1) {
        $error = "Please input an amount above one dollar";
        die($error);
    } else if (is_null($coin) || is_bool($coin) || empty($coin)) {
        $error = "Please select crypto";
        die($error);
    }
    if (!isset($error)) {
        $client = new CoinGeckoClient();
        $coin = trim($coin);
        $list = file_get_contents("../coins.json");
        $arr = json_decode($list);
        foreach ($arr as $value) {
            if (strcasecmp($value->name, $coin) == 0) {
                $symbol = $value->symbol;
                $name = $value->name;
                $id = $value->id;
                break;
            }
        }
        
        $token = PaymentWallet::getTokenPrice($id,$client);
        
        $token = $amount / $token;
       
        $invoice = new Invoice();
        $invoice->setAmount($amount);
        $invoice->setDate(time());
        $invoice->setUser($user->getEmail());
        $invoice->setPackage($name);
        $invoice->setIType($id);
        $invoice->setRoi($token);
        $invoice->setDays(3);
        $invoice->setPaymentMode($name);
        $response = Invoice::createInvoice($invoice, $link);
        if (!is_null($response)) {
            $ww = $user->getEmail();
            User::notify1(EMAIL_SUPPORT, "<p>($ww) has created an invoice with amount $amount USD for $coin deposit</p>", "Invoice Notification");
            $_SESSION['coin'] = $id;
            $_SESSION['amount'] = $amount;
            $address = PaymentWallet::getAllPaymentsWalletsAddressWithSymbol($link, $id);
            $d = $address[array_rand($address)];
            $address = $d->getAddress();
            
            $token = PaymentWallet::getTokenPrice($id, $client);
            $amo = (double) $amount / $token;
            
            $d->text = "We advise that you send exactly USD $amount which is the equivalent of $amo to this wallet address ${address}";
            
            echo $d->text;
         } else {
            $error = "An unexpected error occured.";
            die($error);
        }
    }
}

$news = News::getNewsPage($link);
$cInvoices = Invoice::getCurrentInvoices($link, $user->getEmail());
?>