<?php
namespace App\Models;
use CodeIgniter\Model;

class adDashboardModel extends Model
{
    protected $db;

    public function __construct()
    {
        parent::__construct();
        $this->db = \Config\Database::connect();
    }

    public function getCategoryCount()
    {
        return $this->db->table('categories')->countAllResults();
    }

    public function getProductCount()
    {
        return $this->db->table('products')->countAllResults();
    }

    public function getPendingOrdersCount()
    {
        return $this->db->table('orders')->where('status', 'Pending')->countAllResults();
    }

    public function getApartmentCount()
    {
        return $this->db->table('apartmentdetails')->countAllResults();
    }

    // Fetch products with variant info
    public function getAllProductsWithVariants()
    {
        return $this->db->table('product_variants as pv')
            ->select('pv.*, p.name as product_name, c.category_name')
            ->join('products p', 'p.id = pv.product_id', 'left')
            ->join('categories c', 'c.id = p.category_id', 'left')
            ->orderBy('p.created_at', 'DESC')
            ->get()
            ->getResultArray();
    }

    public function getVariant($id)
    {
        return $this->db->table('product_variants')->where('id', $id)->get()->getRowArray();
    }

    public function updateVariant($id, $data)
    {
        return $this->db->table('product_variants')->where('id', $id)->update($data);
    }
}
