<?php


// File name to create or open
$fileName = "cron.txt";

// The content to write to the file
$contentToWrite = "Hello, this is a test content.";

// Open the file for writing (will create if it doesn't exist)
$fileHandle = fopen($fileName, "w");

// Check if file is opened successfully
if ($fileHandle === false) {
    echo "Error in opening file.";
    exit();
}

// Write the content to the file
fwrite($fileHandle, $contentToWrite);

// Close the file handle
fclose($fileHandle);



date_default_timezone_set("Africa/Lagos");
require_once '/home/u437334197/domains/clockearnings.io/public_html/include/Constants.php';
require_once '/home/u437334197/domains/clockearnings.io/public_html/models/Invoice.php';
require_once '/home/u437334197/domains/clockearnings.io/public_html/include/DatabaseConn.php';
require_once "/home/u437334197/domains/clockearnings.io/public_html/vendor/autoload.php";
require_once "/home/u437334197/domains/clockearnings.io/public_html/models/User.php";

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

function daysSince($unixTimestamp) {
    // Current time in Unix timestamp
    $currentTime = time();

    // Calculating the difference in seconds
    $timeDiff = $currentTime - $unixTimestamp;

    // Converting seconds to days
    $daysPassed = floor($timeDiff / (60 * 60 * 24));

    // If no days have passed, return 0
    return ($daysPassed > 0) ? $daysPassed : 0;
}

$invoices = Invoice::getConfirmedInvoices($link);
if (!is_null($invoices)) {
    foreach ($invoices as $invoice) {
        $status = GROWING;
        $id = $invoice->getId();
        mysqli_query($link, "update invoice set `status` = '$status'  WHERE id = $id");
    }
}

$invoices = Invoice::getCurrentInvoices($link);
$currentTimestamp = time();

if (!is_null($invoices)) {
    foreach ($invoices as $invoice) {
        if (strcmp($invoice->getStatus(), GROWING) == 0) {
            if ($invoice->getCount() < $invoice->getDays()) {
                
                if($invoice->getCountdown() > $currentTimestamp) {
                    $thirtyMinutesLater = $currentTimestamp + (30 * 60);
                    if ($invoice->getCountdown() <= $thirtyMinutesLater) {
                        if($invoice->getNotif() == 1) {
                            $id = $invoice->getId();
                            User::notify20($invoice->getUser(), "Your earnings will be available for withdrawal within the next 30 minutes. ", "Earnings Notice");
                            mysqli_query($link, "update invoice set `notif` = 0 WHERE id = $id");
                        }
                        
                    } 
                }else {
                    $amount = $invoice->getAmount();
                    $roi = $invoice->getRoi();
                    $id = $invoice->getId();
                    $email = $invoice->getUser();
                    $status = REWARD;
                    $iType = $invoice->getItype();
                    $cReturns = $invoice->getCReturns();
                    $wBalance = $invoice->getWBalance();
                    $days = $invoice->getDays();
                    $payable = ($invoice->getRoi() * $invoice->getAmount()) / 100;
                    
                    $cReturns = (float) $cReturns + (float)number_format((float)$payable, 2, '.', '');
                    $count = daysSince($invoice->getConfirmedDate());
                    $date = time();
                    
                    $time = time();
                    $status = "Profit";
                    $wBalance = $wBalance + $payable;
                    mysqli_query($link, "insert INTO wallet(`user`,amount,`date`,status) VALUES('$email',$payable,'$time','$status')");
                    
                    $currentTimestamp = time();

                    // Add 12 hours (43200 seconds) to the current timestamp
                    $timestamp12HoursLater = $currentTimestamp + (12 * 3600);
                    mysqli_query($link, "update invoice set `count` = $count, `countdown` = '$timestamp12HoursLater', `wBalance` = $wBalance, `notif` = 1  WHERE id = $id");
                    
                     if ($count == $days) {
                        $email = $invoice->getUser();
                        $amount = $invoice->getAmount();
                        $status = "Capital";
                        $date = time();
                        
                        mysqli_query($link, "insert into wallet(`user`, amount, `date`, status) VALUES ('$email',$amount,'$date','$status')");
    
                        $status = 'Completed';
                        $newWBal = $wBalance + $invoice->getAmount();
                        mysqli_query($link, "update invoice set `wBalance` = $newWBal, `count` = $count, `status` = '$status'  WHERE id = $id");
                   }

                }

               
            }
        }
    }
}


?>