
<?php

class Referral {

    /**
     * PROPDESCRIPTION
     *
     * @access private
     * @var PROPTYPE
     */
    private $id;

    /**
     * PROPDESCRIPTION
     *
     * @access private
     * @var PROPTYPE
     */
    private $date;

    /**
     * PROPDESCRIPTION
     *
     * @access private
     * @var PROPTYPE
     */
    private $name;

    /**
     * PROPDESCRIPTION
     *
     * @access private
     * @var PROPTYPE
     */
    private $amount;

    /**
     * PROPDESCRIPTION
     *
     * @access private
     * @var PROPTYPE
     */
    private $percentage;

    /**
     * PROPDESCRIPTION
     *
     * @access private
     * @var PROPTYPE
     */
    private $cashOut;

    /**
     * PROPDESCRIPTION
     *
     * @access private
     * @var PROPTYPE
     */
    private $status;

    /**
     * PROPDESCRIPTION
     *
     * @access private
     * @var PROPTYPE
     */
    private $userReferred; // the user that was referred;

    /**
     * PROPDESCRIPTION
     *
     * @access private
     * @var PROPTYPE
     */
    private $user; // the main user;

    /**
     * METHODDESCRIPTION
     *
     * @access public
     * @return RETURNTYPE RETURNDESCRIPTION
     */
    public function getId() {
        return $this->id;
    }

    /**
     * The name of the person referred
     *
     * @access public
     * @return RETURNTYPE RETURNDESCRIPTION
     */
    public function getName() {
        return $this->name;
    }

    /**
     * METHODDESCRIPTION
     *
     * @access public
     * @param ARGTYPE $id ARGDESCRIPTION
     */
    public function setId($id) {
        $this->id = $id;
    }

    /**
     * METHODDESCRIPTION
     *
     * @access public
     * @return RETURNTYPE RETURNDESCRIPTION
     */
    public function getDate() {
        return $this->date;
    }

    /**
     * METHODDESCRIPTION
     *
     * @access public
     * @param ARGTYPE $date ARGDESCRIPTION
     */
    public function setDate($date) {
        $this->date = $date;
    }

    /**
     * METHODDESCRIPTION
     *
     * @access public
     * @return RETURNTYPE RETURNDESCRIPTION
     */
    public function getAmount() {
        return $this->amount;
    }

    /**
     * METHODDESCRIPTION
     *
     * @access public
     * @param ARGTYPE $amount ARGDESCRIPTION
     */
    public function setAmount($amount) {
        $this->amount = $amount;
    }

    /**
     * METHODDESCRIPTION
     *
     * @access public
     * @return RETURNTYPE RETURNDESCRIPTION
     */
    public function getPercentage() {
        return $this->percentage;
    }

    /**
     * METHODDESCRIPTION
     *
     * @access public
     * @param ARGTYPE $percentage ARGDESCRIPTION
     */
    public function setPercentage($percentage) {
        $this->percentage = $percentage;
    }

    /**
     * The user who got referred
     *
     * @access public
     * @return RETURNTYPE RETURNDESCRIPTION
     */
    public function getUserReferred() {
        return $this->userReferred;
    }

    /**
     * The user who got referred
     *
     * @access public
     * @param ARGTYPE $userReferred ARGDESCRIPTION
     */
    public function setUserReferred($userReferred) {
        $this->userReferred = $userReferred;
    }

    /**
     * The main user whose referral link was used.
     *
     * @access public
     * @return RETURNTYPE RETURNDESCRIPTION
     */
    public function getUser() {
        return $this->user;
    }

    /**
     * The main user whose referral link was used.
     *
     * @access public
     * @param ARGTYPE $user ARGDESCRIPTION
     */
    public function setUser($user) {
        $this->user = $user;
    }

    /**
     * The name of the person referred
     *
     * @access public
     * @param ARGTYPE $name ARGDESCRIPTION
     */
    public function setName($name) {
        $this->name = $name;
    }

    public function getCashout() {
        return $this->cashOut;
    }

    public function getStatus() {
        return $this->status;
    }

    public function setCashOut($cashout) {
        $this->cashOut = $cashout;
    }

    public function setStatus($status) {
        $this->status = $status;
    }

    public static function getAllReferrals22($link) {
        $referrals = array();
        $db = new DatabaseConn();
        $link = $db->connect();
        $result = mysqli_query($link, "select referral.`date` as date,referral.status as status, referral.guest as userReferred, users.`name` as name, referral.`user` as `user` from referral inner join users on referral.guest = users.email order by referral.`date` desc");
        if (mysqli_num_rows($result) === 0) {
            return NULL;
        }
        for ($i = 0; $i < mysqli_num_rows($result); $i++) {
            $referral = mysqli_fetch_object($result, Referral::class);
            array_push($referrals, $referral);
            if (mysqli_more_results($link)) {
                mysqli_next_result($link);
            }
        }
        return $referrals;
    }

    public static function getAllReferral($user, $link) {
        $referrals = array();
        $db = new DatabaseConn();
        $link = $db->connect();
        $result = mysqli_query($link, "select referral.`date` as date,referral.status as status, referral.guest as guest, users.`name` as name from referral inner join users on referral.guest = users.email where referral.`user` = '$user'");
        if (mysqli_num_rows($result) === 0) {
            return NULL;
        }
        for ($i = 0; $i < mysqli_num_rows($result); $i++) {
            $referral = mysqli_fetch_object($result, Referral::class);
            array_push($referrals, $referral);
            if (mysqli_more_results($link)) {
                mysqli_next_result($link);
            }
        }
        return $referrals;
    }

    public static function getAllReferral1($user, $link) {
        $referrals = array();
        $db = new DatabaseConn();
        $link = $db->connect();
        $result = mysqli_query($link, "select referral.`date` as date,referral.status as status, referral.guest as guest, users.`name` as name from referral inner join users on referral.guest = users.email where referral.`user` = '$user' order by `date` asc ");
        if (mysqli_num_rows($result) === 0) {
            return NULL;
        }
        for ($i = 0; $i < mysqli_num_rows($result); $i++) {
            $referral = mysqli_fetch_object($result, Referral::class);
            array_push($referrals, $referral);
            if (mysqli_more_results($link)) {
                mysqli_next_result($link);
            }
        }
        return $referrals;
    }

    public static function getReferal($user, $referralId) {

    }

    public static function createReferral($referral) {

    }

    public static function updateReferral($referral, $user) {

    }

    public static function deleteReferral($referralId) {

    }

    public static function getReferralBonus($user, $link) {
        $referrals = Referral::getAllReferral($user, $link);
        $total = 0;

        if (is_null($referrals)) {
            return ($total);
        }
        foreach ($referrals as $referral) {
            $invoices = Invoice::getInvoices($link, $referral->guest);
            if (!is_null($invoices)) {
                foreach ($invoices as $invoice) {
                    if (strcmp($invoice->getStatus(), "Confirmed") == 0 || strcmp($invoice->getStatus(), "Completed") == 0 || strcmp($invoice->getStatus(), "Paused") == 0 || strcmp($invoice->getStatus(), "Growing") == 0) {
                        $total = $total + (0.06 * $invoice->getAmount());
                    }
                }
            }
        }
        return ($total);
    }

    public static function getReferralBonus11($user, $link) {
        $total = 0;
        $invoices = Invoice::getInvoices($link, $user);
        if (!is_null($invoices)) {
            $invoice = $invoices[0];
            if (strcmp($invoice->getStatus(), "Confirmed") == 0 || strcmp($invoice->getStatus(), "Completed") == 0 || strcmp($invoice->getStatus(), "Paused") == 0 || strcmp($invoice->getStatus(), "Growing") == 0) {
                $total = $invoice->getAmount() * 0.06;
               
            }
        }
        return $total;
    }

    public static function getReferralBonus1($user, $link) {
        $referrals = Referral::getAllReferral($user, $link);
        $total = 0;

        if (is_null($referrals)) {
            return ($total);
        }
        $referral = $referrals[0];
        $invoices = Invoice::getInvoices($link, $referral->guest);
        if (!is_null($invoices)) {
            foreach ($invoices as $invoice) {
                if (strcmp($invoice->getStatus(), "Confirmed") == 0 || strcmp($invoice->getStatus(), "Completed") == 0 || strcmp($invoice->getStatus(), "Paused") == 0 || strcmp($invoice->getStatus(), "Growing") == 0) {
                    $total = $total + (0.06 * $invoice->getAmount());
                    break;
                }
            }
        }
        return ($total);
    }

    public static function getRefAmount($user, $link) {
        $referrals = Referral::getAllReferral($user, $link);
        $total = 0;

        if (is_null($referrals)) {
            return ($total);
        }
        foreach ($referrals as $referral) {
            $invoices = Invoice::getInvoices($link, $referral->guest);
            if (!is_null($invoices)) {
                foreach ($invoices as $invoice) {
                    if (strcmp($invoice->getStatus(), "Confirmed") == 0 || strcmp($invoice->getStatus(), "Completed") == 0 || strcmp($invoice->getStatus(), "Paused") == 0 || strcmp($invoice->getStatus(), "Growing") == 0) {
                        $total = $invoice->getAmount() + $total;
                        break;
                    }
                }
            }
        }
        return ($total);
    }

}
