<?php
defined('BASEPATH') or exit('No direct script access allowed');

class M_Coinflip extends CI_Model
{
    public function __construct()
    {
        parent::__construct();
        $this->load->database();
    }

    public function getHistory($userId)
    {
        $this->db->order_by('id', 'DESC');
        return $this->db->get_where('coinflip_history', array('user_id' => $userId))->result_array();
    }

    public function insertHistory($userId, $userCoin, $resultCoin, $betAmount, $profitLoss)
    {
        $data = array(
            'user_id' => $userId,
            'user_coin' => $userCoin,
            'result_coin' => $resultCoin,
            'bet_amount' => $betAmount,
            'profit_loss' => $profitLoss,
            'timestamp' => time()
        );

        $this->db->insert('coinflip_history', $data);

        return $this->db->insert_id();
    }

    public function addBalance($userId, $amount)
    {
        $this->db->set('balance', 'balance+' . $amount, false);
        $this->db->where('id', $userId);
        $this->db->update('users');
    }

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