<?php
if (!defined('BASEPATH'))
    exit('No direct script access allowed');
class Home_model extends CI_Model
{

    public function count_new_orders($branch_id = '', $from_app='')
    {
        // print_R($from_app);
        $res = $this->db->select('count(id) as counter');

        // Fetch user_id from session
        if ($from_app != '1') {
            $user_id = $this->session->userdata('user_id');

            // Check if the user is a rider
            if ($this->ion_auth->is_rider()) {
                $this->db->where('o.rider_id', $user_id);
            }
        }

        // Add branch_id condition if provided
        if (!empty($branch_id)) {
            $this->db->where('o.branch_id', $branch_id);
        }

        $res = $this->db->get('orders o')->result_array();
        // print_r($this->db->last_query());
        // Ensure a counter value is always returned
        return isset($res[0]['counter']) ? $res[0]['counter'] : 0;
    }


    public function count_orders_by_status($status, $branch_id = '')
    {
        $this->db->select('COUNT(id) as counter');
        $this->db->where('active_status', $status);

        // Add branch_id condition if provided
        if (!empty($branch_id)) {
            $this->db->where('branch_id', $branch_id);
        }

        $res = $this->db->get('orders o')->result_array();
        return $res[0]['counter'];
    }


    public function count_new_users()
    {
        $res = $this->db->select('count(u.id) as counter')->join('users_groups ug', ' ug.`user_id` = u.`id` ')
            ->where('ug.group_id=2')
            ->get('`users u`')->result_array();
        return $res[0]['counter'];
    }

    public function count_riders($branch_id = '')
    {
        $this->db->select('COUNT(u.id) as counter');
        $this->db->where('ug.group_id', '3');
        $this->db->join('users_groups ug', 'ug.user_id = u.id');

        // Add branch_id condition if provided
        if (!empty($branch_id)) {
            $this->db->where('u.branch_id', $branch_id);
        }

        $res = $this->db->get('users u')->result_array();
        return $res[0]['counter'];
    }

    public function count_branch()
    {
        // if (isset($_SESSION['branch_id'])) { // no need of  session id here if response is null check not null condidtion in header direct 

            $res = $this->db->select('count(b.id) as counter')->get('`branch` b')->result_array();
            // print_r($res);
            return $res[0]['counter'];
        // }
    }

    public function count_products($branch_id = "")
    {
        $res = $this->db->select('count(id) as counter ');
        if (!empty($branch_id) && $branch_id != '') {
            $res->where('branch_id=' . $branch_id);
        }
        $count = $res->get('`products`')->result_array();
        return $count[0]['counter'];
    }
    public function count_tags($partner_id = "")
    {
        $res = $this->db->select('count(id) as counter ');
        if (!empty($partner_id) && $partner_id != '') {
            $res->where('partner_id=' . $partner_id);
        }
        $count = $res->get('`partner_tags`')->result_array();
        return $count[0]['counter'];
    }

    public function count_products_stock_low_status($branch_id = "")
    {
        $settings = get_settings('system_settings', true);
        $low_stock_limit = isset($settings['low_stock_limit']) ? $settings['low_stock_limit'] : 5;
        $count_res = $this->db->select(' COUNT( distinct(p.id)) as `total` ')->join('product_variants', 'product_variants.product_id = p.id');
        $where = "p.stock_type is  NOT NULL";

        $count_res->where($where);
        $count_res->where('p.stock  <=', $low_stock_limit);
        $count_res->where('p.availability  =', '1');
        $count_res->or_where('product_variants.stock  <=', $low_stock_limit);
        $count_res->where('product_variants.availability  =', '1');
        if (!empty($branch_id) && $branch_id != '') {
            $count_res->where('p.branch_id  =', $branch_id);
        }
        $product_count = $count_res->get('products p')->result_array();
        // print_R($this->db->last_query());
        return $product_count[0]['total'];
    }

    public function count_products_availability_status($branch_id = "")
    {
        $count_res = $this->db->select(' COUNT( distinct(p.id)) as `total` ')->join('product_variants', 'product_variants.product_id = p.id');
        $where = "p.stock_type is  NOT NULL";
        if (!empty($branch_id) && $branch_id != '') {
            $count_res->where('p.branch_id  =', $branch_id);
        }
        $count_res->where($where);
        $count_res->where('p.stock ', '0');
        $count_res->where('p.availability ', '0');
        $count_res->or_where('product_variants.stock ', '0');
        $count_res->where('product_variants.availability', '0');
        $product_count = $count_res->get('products p')->result_array();
        return $product_count[0]['total'];
    }
    public function total_earnings($type = "admin")
    {
        $select = "";
        if ($type == "admin") {
            $select = "SUM(admin_commission_amount) as total ";
        }
        if ($type == "partner") {
            $select = "SUM(partner_commission_amount) as total ";
        }
        if ($type == "overall") {
            $select = "SUM(final_total) as total ";
        }
        $count_res = $this->db->select($select);
        $where = "is_credited=1";
        $count_res->where($where);

        $product_count = $count_res->get('orders')->result_array();
        return $product_count[0]['total'];
    }
}
