<?php
defined('BASEPATH') or exit('No direct script access allowed');
class M_Withdraw extends CI_Model
{

    public function checkSameWallet($userId, $wallet)
    {
        // Bound parameters: correct value comparison (the old version compared
        // against the literal string ".$wallet." and never matched anything)
        // and safe against SQL injection.
        $check = $this->db->query(
            "SELECT DISTINCT user_id FROM withdraw_history WHERE user_id <> ? AND wallet = ?",
            [$userId, $wallet]
        );
        if ($check->num_rows() == 0) {
            return false;
        }
        return $check->result_array();
    }

    public function getTotalWithdrawToday($userId)
    {
        $todayMidnight = strtotime('today midnight');
        return $this->db->query("SELECT IFNULL(SUM(amount),0) as amt FROM withdraw_history WHERE claim_time > " . $todayMidnight . " AND user_id = " . $userId)->result_array()[0]['amt'];
    }

    public function reduceBalance($id, $amount)
    {
        $this->db->set('balance', 'balance-' . $amount, FALSE);
        $this->db->where('id', $id);
        $this->db->update('users');
    }

    public function get_ref($id)
    {
        return $this->db->get_where('users', array('referred_by' => $id))->num_rows();
    }
    public function insert_history($id, $type, $method, $wallet, $amount)
    {
        $insert = array(
            'user_id' => $id,
            'type' => $type,
            'method' => $method,
            'wallet' => $wallet,
            'ip_address' => $this->input->ip_address(),
            'amount' => $amount,
            'claim_time' => time()
        );
        $this->db->insert('withdraw_history', $insert);
    }

    public function update_wallet($user_id, $wallet)
    {
        $this->db->where('id', $user_id);
        $this->db->set('wallet', $wallet);
        $this->db->update('users');
    }
		public function get_faucet_history($id)

	{

		$this->db->order_by('id', "desc")->limit(20);

		return $this->db->get_where('faucet_history', array('user_id' => $id))->result_array();

	}



	public function get_other_history($id)

	{

		$this->db->order_by('id', "desc")->limit(50);

		return $this->db->get_where('other_history', array('user_id' => $id))->result_array();

	}



	public function get_wheel_history($id)

	{

		$this->db->order_by('id', "desc")->limit(20);

		return $this->db->get_where('wheel_history', array('user_id' => $id))->result_array();

	}



	public function get_shortlinks_history($id)

	{

		$this->db->order_by('id', "desc")->limit(20);

		return $this->db->get_where('link_history', array('user_id' => $id))->result_array();

	}



	public function get_ptc_history($id)

	{

		$this->db->order_by('id', "desc")->limit(20);

		return $this->db->get_where('ptc_history', array('user_id' => $id))->result_array();

	}



	public function get_lottery_history($id)

	{

		$this->db->order_by('id', "desc")->limit(20);

		return $this->db->get_where('lottery_winners', array('user_id' => $id))->result_array();

	}

	public function getReferrals($userId)

	{

		$this->db->order_by('id', "desc")->limit(100);

		return $this->db->get_where('users', ['referred_by' => $userId])->result_array();

	}

	public function get_offerwall_history($id)

	{

		$this->db->order_by('claim_time', "desc")->limit(20);

		return $this->db->get_where('offerwall_history', array('user_id' => $id))->result_array();

	}

	public function get_withdrawals_history($id)

	{

		$this->db->order_by('id', "desc")->limit(20);

		return $this->db->get_where('withdraw_history', array('user_id' => $id))->result_array();

	}

	public function get_task_history($id)

	{

		$this->db->order_by('id', "desc")->limit(20);

		return $this->db->query("SELECT tasks.name, task_history.id, tasks.usd_reward, task_history.claim_time FROM task_history, tasks WHERE task_history.user_id = " . $id . " AND task_history.task_id = tasks.id")->result_array();

	}
    public function findCheatLog($userId)
    {
        $check = $this->db->get_where('cheat_logs', ['user_id' => $userId]);
        return $check->num_rows() != 0;
    }
    public function readNotification($userId)
    {
        $this->db->set('status', 1);
        $this->db->where('user_id', $userId);
        $this->db->update('notifications');
    }

    public function processPayment($paymentId)
    {
        $this->db->set('type', 1);
        $this->db->where('id', $paymentId);
        $this->db->update('withdraw_history');
    }

    public function readAllNotifications($userId)
    {
        $this->db->set('status', 1);
        $this->db->where('user_id', $userId);
        $this->db->update('notifications');
    }

    public function updateFPHash($userId, $hash)
    {
        $this->db->where('id', $userId);
        $this->db->set('fp_hash', $hash);
        $this->db->update('users');
    }
}