<?php
// require_once('basic-function.php');
class user extends basicFunction
{
    use table_names;
    use mail;

    public function create($full_name, $email, $referrer, $password, $c_password)
    {
        // "user_full_name": "user_email": "user_name": "user_referrer": "user_phone": "user_address": "user_country": "user_password": "user_confirm_pass"

        $vals = [$full_name, $email, $password, $c_password];
        $if_empty = $this->if_empty($vals);
        if (!$if_empty) {
            return $this->errorMsgs(1);
        }

        $chk_full_name = explode(' ', trim($full_name));
        if (count($chk_full_name) < 2) {
            return $this->errorMsgs(20);
        }

        // valid email
        $valid_email = $this->validate_email($email);
        if (!$valid_email) {
            return $this->errorMsgs(18);
        }

        $if_exist_email = $this->selectWhereDlt($this->user_tb, 'email', $email);
        // return $if_exist_email;
        if (count($if_exist_email) > 0) {
            return $this->errorMsgs(8);
        }

        // $if_exist_username = $this->selectWhereDlt($this->user_tb,'username',$username);
        // if(count($if_exist_username) > 0){
        //     return $this->errorMsgs(19);
        // }
        $referrer_id = null;
        if (!empty($referrer)) {
            $if_exist_referrer = $this->selectWhereDlt($this->user_tb, 'unique_id', $referrer);
            if (count($if_exist_referrer) < 1) {
                return $this->errorMsgs(9);
            } else {
                $referrer_id = $if_exist_referrer[0]['unique_id'];
            }
        }
        if ($password !== $c_password) {
            return $this->errorMsgs(2);
        }
        $name_arr = explode(' ', $full_name);
        $insert_name = implode('++', $name_arr);
        $hash_pass = $this->hashPass($password);
        $unique_id = $this->rand_id();
        $date_created = $this->todayDate();
        $query = "INSERT INTO $this->user_tb VALUES (null,
        '" . mysqli_real_escape_string($this->connectDb(), $unique_id) . "',
        '" . mysqli_real_escape_string($this->connectDb(), $email) . "',
        '" . mysqli_real_escape_string($this->connectDb(), $insert_name) . "',
        '" . mysqli_real_escape_string($this->connectDb(), null) . "',
        '" . mysqli_real_escape_string($this->connectDb(), $hash_pass) . "',
        '" . mysqli_real_escape_string($this->connectDb(), 0) . "',
        '" . mysqli_real_escape_string($this->connectDb(), 0) . "',
        '" . mysqli_real_escape_string($this->connectDb(), 0) . "',
        '" . mysqli_real_escape_string($this->connectDb(), $date_created) . "',
        '" . mysqli_real_escape_string($this->connectDb(), $date_created) . "') ";
        mysqli_close($this->connectDb());
        if ($this->sqlQuery($query)) {

            $user_details = $this->create_user_details($unique_id, null, null, null, $referrer_id);
            if ($user_details['error'] == 1) {
                return $user_details;
            }
            // $user_wallet = $this->create_wallet($unique_id);
            // if ($user_wallet['error'] == 1) {
            //     return $user_wallet;
            // }


            // $fields = ['address'];
            // $values = [$btc_address];

            // $insert_btc_address = $this->update_where($this->user_wallet_tb, $fields, $values, 'user_id', $unique_id);
            // if ($insert_btc_address['error'] == 1) {
            //     return $insert_btc_address;
            // }

            $this->welcomeEmail($email, $name_arr[0]);
            return $this->successMsg('Your Account Was');
        } else {
            return $this->errorMsgs(14);
        }
    }

    public function create_user_details($user_id, $phone, $address, $country, $referrer)
    {
        $date_created = $this->todayDate();
        $query = "INSERT INTO $this->user_details VALUES (null,
        '" . mysqli_real_escape_string($this->connectDb(), $user_id) . "',
        '" . mysqli_real_escape_string($this->connectDb(), $phone) . "',
        '" . mysqli_real_escape_string($this->connectDb(), $address) . "',
        '" . mysqli_real_escape_string($this->connectDb(), $country) . "',
        '" . mysqli_real_escape_string($this->connectDb(), 'avatar.png') . "',
        '" . mysqli_real_escape_string($this->connectDb(), $referrer) . "',
        '" . mysqli_real_escape_string($this->connectDb(), 0) . "',
        '" . mysqli_real_escape_string($this->connectDb(), $date_created) . "',
        '" . mysqli_real_escape_string($this->connectDb(), $date_created) . "') ";
        mysqli_close($this->connectDb());
        if ($this->sqlQuery($query)) {
            return $this->successMsg('User details');
        } else {
            return $this->errorMsgs(14);
        }
    }
    public function insert_user_wallet($user_id, $coin_id)
    {
        $unique_id = $this->rand_id();
        $date_created = $this->todayDate();
        $query = "INSERT INTO $this->user_wallet_tb VALUES (null,
        '" . mysqli_real_escape_string($this->connectDb(), $unique_id) . "',
        '" . mysqli_real_escape_string($this->connectDb(), $user_id) . "',
        '" . mysqli_real_escape_string($this->connectDb(), $coin_id) . "',
        '" . mysqli_real_escape_string($this->connectDb(), null) . "',
        '" . mysqli_real_escape_string($this->connectDb(), 0) . "',
        '" . mysqli_real_escape_string($this->connectDb(), $date_created) . "',
        '" . mysqli_real_escape_string($this->connectDb(), $date_created) . "') ";
        mysqli_close($this->connectDb());
        if ($this->sqlQuery($query)) {
        } else {
            return $this->errorMsgs(14);
        }
    }

    public function edit($name, $country, $address, $email, $phone, $user_id)
    {
        $vals = [$name, $email, $country, $address, $email, $phone];
        $if_empty = $this->if_empty($vals);
        if (!$if_empty) {
            return $this->errorMsgs(1);
        }

        $chk_full_name = explode(' ', trim($name));
        if (count($chk_full_name) < 2) {
            return $this->errorMsgs(20);
        }
        // $user_id = $_SESSION[$this->user_session];

        // valid email
        $valid_email = $this->validate_email($email);
        if (!$valid_email) {
            return $this->errorMsgs(18);
        }

        $if_exist_email = $this->selectWhereDlt($this->user_tb, 'email', $email);
        if (count($if_exist_email) > 0 && $if_exist_email[0]['unique_id'] !== $user_id) {
            return $this->errorMsgs(8);
        }
        $fields = ['phone', 'address', 'country'];
        $values = [$phone, $address, $country];

        $update_user_details = $this->update_where($this->user_details, $fields, $values, 'user_unique_id', $user_id);
        if ($update_user_details['error'] == 1) {
            return $update_user_details;
        }
        $insert_name = implode('++', $chk_full_name);
        $user_fields = ['email', 'full_name'];
        $user_values = [$email, $insert_name];
        $update_user = $this->update_tbs($this->user_tb, $user_fields, $user_values, $user_id);
        if ($update_user['error'] == 1) {
            return $update_user;
        }

        return $this->successMsg2('Profile Update');
    }

    public function profile_image($file, $id)
    {
        $vals = [$file];
        $if_empty = $this->if_empty($vals);
        if (!$if_empty) {
            return $this->errorMsgs(12);
        }
        // $user = $this->getDataDlt($id,$this->user_details);
        $user = $this->selectWhereDlt($this->user_details, 'user_unique_id', $id);
        if (count($user) < 1) {
            return $this->errorMsgs(15);
        }
        if ($file !== null) {

            $chk_extension = $this->chk_file_extension($this->valid_img_extensions, $file);
            if (!$chk_extension) {
                return $this->errorMsgs(12);
            }
            $file_name = $this->rand_id();
            $upload_img = $this->file_upload_custom($file, 'assets/uploads/profile/', $this->file_max_size, $file_name);
            if ($upload_img['error'] == 1) {
                return $upload_img;
            }
            $update_image = $this->update_where($this->user_details, ['passport'], [$upload_img['file_name']], 'user_unique_id', $id);
            if ($update_image['error'] == 1) {
                return $update_image;
            } else {
                if ($user[0]['passport'] !== 'avatar.png') {
                    unlink('assets/uploads/profile/' . $user[0]['passport']);
                }
                return $this->successMsg2('Profile Image Upload');
            }
        } else {
            return $this->errorMsgs(15);
        }
    }

    public function create_wallet($user_id)
    {
        $vals = [$user_id];
        $if_empty = $this->if_empty($vals);
        if (!$if_empty) {
            return $this->errorMsgs(15);
        }
        $all_coins = $this->selectAllDlt($this->currencies_tb);
        if (count($all_coins) < 1) {
            return $this->successMsg2('Wallet Added');
        }

        foreach ($all_coins as $e) {
            $insert = $this->insert_user_wallet($user_id, $e['unique_id']);
            if ($insert['error'] == 1) {
                return $insert;
            }
        }
        return $this->successMsg('Wallet');
    }
    public function add_wallet_address($coin_id, $address)
    {
        $vals = [$address];
        $if_empty = $this->if_empty($vals);
        if (!$if_empty) {
            return $this->errorMsgs(1);
        }
        $vals = [$coin_id];
        $if_empty = $this->if_empty($vals);
        if (!$if_empty) {
            return $this->errorMsgs(15);
        }
        $user_id = $_SESSION[$this->user_session];

        $wallet_exist = $this->selectWhere2CondDlt($this->user_wallet_tb, 'coin_id', $coin_id, 'user_id', $user_id);
        if (count($wallet_exist) < 1) {
            return $this->errorMsgs(15);
        }

        $fields = ['address'];
        $values = [$address];
        $update_user = $this->update_where($this->user_wallet_tb, $fields, $values, 'unique_id', $wallet_exist[0]['unique_id']);
        if ($update_user['error'] == 1) {
            return $update_user;
        }
        return $this->successMsg2('Wallet Added');
    }
    public function block($id)
    {
        $vals = [$id];
        $if_empty = $this->if_empty($vals);
        if (!$if_empty) {
            return $this->errorMsgs(15);
        }
        $fields = ['is_blocked'];
        $values = ['1'];
        $block_user = $this->update_tbs($this->user_tb, $fields, $values, $id);
        if ($block_user['error'] == 1) {
            return $block_user;
        }
        return $this->successMsg3('User Blocked');
    }
    public function unblock($id)
    {
        $vals = [$id];
        $if_empty = $this->if_empty($vals);
        if (!$if_empty) {
            return $this->errorMsgs(15);
        }
        $fields = ['is_blocked'];
        $values = ['0'];
        $block_user = $this->update_tbs($this->user_tb, $fields, $values, $id);
        if ($block_user['error'] == 1) {
            return $block_user;
        }
        return $this->successMsg3('User Account Activated');
    }
    public function delete($id)
    {
        $vals = [$id];
        $if_empty = $this->if_empty($vals);
        if (!$if_empty) {
            return $this->errorMsgs(15);
        }
        $fields = ['is_deleted'];
        $values = ['1'];
        $delete_user = $this->update_tbs($this->user_tb, $fields, $values, $id);
        if ($delete_user['error'] == 1) {
            return $delete_user;
        }
        $user_details = $this->selectWhereDlt($this->user_details, 'user_unique_id', $id);
        if (count($user_details) > 0) {
            $fields = ['is_deleted'];
            $values = ['1'];
            $update_details = $this->update_where($this->user_details, $fields, $values, 'user_unique_id', $id);
            if ($update_details['error'] == 1) {
                return $update_details;
            }
        }
        $user_wallet = $this->selectWhereDlt($this->user_wallet_tb, 'user_id', $id);
        if (count($user_wallet) > 0) {
            $fields = ['is_deleted'];
            $values = ['1'];
            $update_wallet = $this->update_where($this->user_wallet_tb, $fields, $values, 'user_id', $id);
            if ($update_wallet['error'] == 1) {
                return $update_wallet;
            }
        }
        $user_withdrawal = $this->selectWhereDlt($this->withdrawal_tb, 'user_id', $id);
        if (count($user_withdrawal) > 0) {
            $fields = ['is_deleted'];
            $values = ['1'];
            $update_withdrawal = $this->update_where($this->withdrawal_tb, $fields, $values, 'user_id', $id);
            if ($update_withdrawal['error'] == 1) {
                return $update_withdrawal;
            }
        }
        $user_txn = $this->selectWhereDlt($this->transaction_tb, 'user_id', $id);
        if (count($user_txn) > 0) {
            $fields = ['is_deleted'];
            $values = ['1'];
            $update_txn = $this->update_where($this->transaction_tb, $fields, $values, 'user_id', $id);
            if ($update_txn['error'] == 1) {
                return $update_txn;
            }
        }
        $user_support = $this->selectWhereDlt($this->support_tb, 'user_id', $id);
        if (count($user_support) > 0) {
            $fields = ['is_deleted'];
            $values = ['1'];
            $update_support = $this->update_where($this->support_tb, $fields, $values, 'user_id', $id);
            if ($update_support['error'] == 1) {
                return $update_support;
            }
        }
        $user_referrer = $this->selectWhereDlt($this->user_details, 'referrer', $id);
        if (count($user_referrer) > 0) {
            $fields = ['referrer'];
            $values = [null];
            $update_referrer = $this->update_where($this->user_details, $fields, $values, 'referrer', $id);
            if ($update_referrer['error'] == 1) {
                return $update_referrer;
            }
        }
        return $this->dltSuccessMsg();
    }

    public function total_balance($user_id)
    {
        $balance = 0;
        $all_deposits = $this->selectWhereDlt($this->transaction_tb, 'user_id', $user_id);
        foreach ($all_deposits as $e) {
            $balance += $e['interest_in_usd'];
        }
        return round($balance, 3);
    }
    public function total_bonus($user_id)
    {
        $balance = 0;
        $all_deposits = $this->selectWhere2CondDlt($this->wallet_tb, 'user_id', $user_id, 'description', 'affiliate_bonus');
        foreach ($all_deposits as $e) {
            $balance += $e['amt_in_usd'];
        }
        return round($balance, 3);
    }
    public function no_referred($user_id)
    {
        $all_referrers = $this->selectWhereDlt($this->user_details, 'referrer', $user_id);
        return count($all_referrers);
    }
    public function pending_withdrawals($user_id)
    {
        $balance = 0;
        $all_withdrawals = $this->selectWhere2CondDlt($this->wallet_tb, 'user_id', $user_id, 'status', 'pending');
        foreach ($all_withdrawals as $e) {
            $balance += $e['amt_in_usd'];
        }
        return round($balance, 3);
    }
    public function confirmed_deposits($user_id)
    {
        $balance = 0;
        $all_withdrawals = $this->selectWhere2CondDlt($this->transaction_tb, 'user_id', $user_id, 'status', 'confirmed');
        foreach ($all_withdrawals as $e) {
            $balance += $e['amt_in_usd'];
        }
        return round($balance, 3);
    }
    public function confirm_old_pass_reset($old_pass, $user_id)
    {
        if (!isset($old_pass)) {
            return ['error' => 1, 'msg' => 'Please enter your current password'];
        }
        $hashedpass = $this->hashPass($old_pass);
        $row = $this->selectWhere2CondDlt($this->user_tb, 'unique_id', $user_id, 'password', $hashedpass);
        if (count($row) > 0) {
            return ['error' => 0, 'msg' => 'Welcome '];
        } else {
            return ['error' => 1, 'msg' => 'Current Password is invalid'];
        }
    }
    public function reset_password($user_id, $password, $c_password)
    {
        $vals = [$user_id];
        $if_empty = $this->if_empty($vals);
        if (!$if_empty) {
            return $this->errorMsgs(15);
        }
        $vals = [$password, $c_password];
        $if_empty = $this->if_empty($vals);
        if (!$if_empty) {
            return $this->errorMsgs(1);
        }
        if ($password !== $c_password) {
            return $this->errorMsgs(2);
        }
        $hash_pass = $this->hashPass($password);
        $date_created = $this->todayDate();
        $fields = ['password', 'updated_at'];
        $values = [$hash_pass, $date_created];
        $update_password = $this->update_tbs($this->user_tb, $fields, $values, $user_id);
        if ($update_password['error'] == 1) {
            return $update_password;
        }
        return $this->successMsg2('Password Reset');
    }

    public function forgot_password($email)
    {
        $vals = [$email];
        $if_empty = $this->if_empty($vals);
        if (!$if_empty) {
            return $this->errorMsgs(1);
        }
        $valid_email = $this->validate_email($email);
        if (!$valid_email) {
            return $this->errorMsgs(18);
        }
        $user_exist = $this->selectWhereDlt($this->user_tb, 'email', $email);
        if (count($user_exist) < 1) {
            return $this->errorMsgs(11);
        }
        // return $this->successMsg3('User Confirmed');
        return [
            'error' => 0,
            'user_id' => $user_exist[0]['unique_id'],
        ];
    }
}
$user = new user();
