<?php
require_once('table-traits.php');
require_once('basic-function.php');
require_once('user-functions.php');
require_once('wallet-functions.php');
require_once('ProductController.php');

class Purchase extends basicFunction
{
    private $user;
    private $wallet;
    private $product;

    public function __construct()
    {
        $this->user = new user();
        $this->wallet = new wallet();
        $this->product = new Product();
    }

    public function create($product_id)
    {
        $vals = [$product_id];
        $if_empty = $this->if_empty($vals);
        if (!$if_empty) {
            return $this->errorMsgs(1);
        }
        $product = ($this->selectWhereDlt($this->product_tb, 'unique_id', $product_id))[0];
        if ($product['status'] === 'Sold Out') {
            return ['error' => 1, 'msg' => 'Error! Product is sold out.'];
        }

        $user_logged = $this->loggedInAuth($this->user->user_tb, 'unique_id', $this->user->user_session, '../en/');
        $user_balance = $this->wallet->get_balance($user_logged['unique_id']);
        if ($user_balance < $product['price']) {
            return $this->errorMsgs(21);
        }

        $currency = ($this->selectAllDlt($this->currencies_tb))[0];


        $unique_id = $this->rand_id();
        $date_created = $this->todayDate();
        $query = "INSERT INTO $this->purchase_tb VALUES (null,
    '" . mysqli_real_escape_string($this->connectDb(), $unique_id) . "',
    '" . mysqli_real_escape_string($this->connectDb(), $product['category_id']) . "',
    '" . mysqli_real_escape_string($this->connectDb(), $product['subcategory_id']) . "',
    '" . mysqli_real_escape_string($this->connectDb(), $product_id) . "',
    '" . mysqli_real_escape_string($this->connectDb(), $user_logged['unique_id']) . "',
    '" . mysqli_real_escape_string($this->connectDb(), 'pending') . "',
    '" . 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->errorMsgs(14);
        } else {
            $this->product->update($product['unique_id'], $product['category_id'], $product['subcategory_id'], 'Sold Out', $product['type'], $product['includes'], $product['balance'], $product['price']);

            $this->wallet->remove_investment_From_Wallet($user_logged, $currency['unique_id'], $product['price']);
        }
        return $this->successMsg('Purchase');
    }

    public function update($id, $category_id, $subcategory_id, $status, $type, $includes, $balance, $price)
    {
        $vals = [$category_id, $subcategory_id, $status, $type, $includes, $balance, $price];
        $if_empty = $this->if_empty($vals);
        if (!$if_empty) {
            return $this->errorMsgs(1);
        }


        $fields = ['status', 'type', 'includes', 'balance', 'price'];
        $values = [$status, $type, $includes, $balance, $price];
        $update = $this->update_tbs($this->product_tb, $fields, $values, $id);
        if ($update['error'] == 1) {
            return $update;
        }

        return $this->successMsg3('Product Updated');
    }


    public function delete($id)
    {
        $vals = [$id];
        $if_empty = $this->if_empty($vals);
        if (!$if_empty) {
            return $this->errorMsgs(15);
        }

        $fields = ['is_deleted'];
        $values = ['1'];
        $subcategory = $this->update_where($this->product_tb, $fields, $values, 'unique_id', $id);
        if ($subcategory['error'] == 1) {
            return $subcategory;
        }

        return $this->dltSuccessMsg();
    }


    public function status($id)
    {
        $vals = [$id];
        $if_empty = $this->if_empty($vals);
        if (!$if_empty) {
            return $this->errorMsgs(15);
        }

        $status = null;
        $purchase = ($this->selectWhereDlt($this->purchase_tb, 'unique_id', $id))[0];
        if ($purchase['status'] === 'pending') {
            $status = 'confirmed';
        } else {
            $status = 'pending';
        }

        $fields = ['status'];
        $values = [$status];
        $subcategory = $this->update_where($this->purchase_tb, $fields, $values, 'unique_id', $id);
        if ($subcategory['error'] == 1) {
            return $subcategory;
        }

        return $this->successMsg3('Status Updated');
    }



    public function total_purchase_amount($user_id)
    {
        $total = 0;
        $purchases = $this->selectWhere2Cond($this->purchase_tb, 'user_id', $user_id, 'status', 'confirmed');
        if (count($purchases) > 0) {
            foreach ($purchases as $e) {
                $product = ($this->selectWhere($this->product_tb, 'unique_id', $e['product_id'])[0]);
                $total += $product['price'];
            }
        }
        return $total;
    }

    public function pending_purchase_amount($user_id)
    {
        $total = 0;
        $purchases = $this->selectWhere2Cond($this->purchase_tb, 'user_id', $user_id, 'status', 'pending');
        if (count($purchases) > 0) {
            foreach ($purchases as $e) {
                $product = ($this->selectWhere($this->product_tb, 'unique_id', $e['product_id'])[0]);
                $total += $product['price'];
            }
        }
        return $total;
    }

    public function admin_total_purchase_amount()
    {
        $total = 0;
        $purchases = $this->selectWhere($this->purchase_tb, 'status', 'confirmed');
        if (count($purchases) > 0) {
            foreach ($purchases as $e) {
                $product = ($this->selectWhere($this->product_tb, 'unique_id', $e['product_id'])[0]);
                $total += $product['price'];
            }
        }
        return $total;
    }
    public function admin_total_available_product()
    {
        $total = 0;
        $purchases = $this->selectWhere($this->product_tb, 'status', 'available');
        if (count($purchases) > 0) {
            foreach ($purchases as $e) {
                $total++;
            }
        }
        return $total;
    }
}

$purchase = new Purchase();
