<?php

class Withdrawal {

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

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

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

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

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

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

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

    public function __construct() {

    }

    /**
     * Returns the email of the user
     *
     * @access public
     * @return RETURNTYPE RETURNDESCRIPTION
     */
    public function getUser() {
        return $this->user;
    }

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

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

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

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

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

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

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

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

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

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

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

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

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

    /**
     * Get all withdrawals on the system, if email is provided
     * It fetches the withdrawal logs of the particular user.
     *
     * @param mysqli $link
     * @access public
     *
     * @return Withdrawal Array of withdrawals or null;
     */
    public static function getWithdrawals($link, $email = NULL) {
        if (!$link instanceof mysqli) {
            throw new Exception("Bad arguments passed: \n");
        }
        $status = INVESTED;
        if (is_null($email)) {
            $query = "select * from withdrawals WHERE status != '$status'";
        } else {
            $query = "select * from withdrawals WHERE `user` = '$email' and status != '$status'";
        }
        $withdraws = array();
        $result = mysqli_query($link, $query);
        if (mysqli_num_rows($result) === 0) {
            return NULL;
        }
        for ($i = 0; $i < mysqli_num_rows($result); $i++) {
            $withdraw = mysqli_fetch_object($result, Withdrawal::class);
            array_push($withdraws, $withdraw);
            if (mysqli_more_results($link)) {
                mysqli_next_result($link);
            }
        }
        return $withdraws;
    }

}
