<?php

namespace Hura8\Components\Product\Model;

use Hura8\Interfaces\AppResponse;
use Hura8\System\Model\aEntityBaseModel;

class ProductSpecGroupModel extends aEntityBaseModel
{


    protected $tb_product                = 'tb_product';
    protected $tb_product_spec_group     = 'tb_product_spec_group';
    protected $tb_product_attribute      = 'tb_product_attribute';

    public function __construct() {
        parent::__construct('product_spec_group');
    }


    protected function extendedFilterOptions() : array
    {
        return [
            // empty for now
        ];
    }


    public function getSpecGroupAttribute($group_id) {
        $objProductSpecGroupAttributeModel = new ProductSpecGroupAttributeModel();
        return $objProductSpecGroupAttributeModel->getSpecGroupAttribute($group_id);
    }


    protected function clearProductAttributes($product_id) {
        $this->db->runQuery(
            "DELETE FROM `".$this->tb_product_attribute."` WHERE `pro_id` = ? ",
            [ 'd' ], [ $product_id ]
        );
    }


    public function clearProductSpecGroup($product_id) {

        $this->clearProductAttributes($product_id);

        $product_spec_group_id = $this->getProductSpecGroupId($product_id);

        // update
        $this->db->runQuery(
            "UPDATE `".$this->tb_product."` SET 
                            `spec_group_id` = 0
                        WHERE `id` = ? LIMIT 1 ",
            [ 'd' ], [ $product_id ]
        );

        $this->updateSpecGroupProductCount($product_spec_group_id);

        return true;
    }


    public function setProductSpecGroup($product_id, $new_group_id)
    {
        $current_group_id = $this->getProductSpecGroupId($product_id);

        if($current_group_id != $new_group_id) {
            // update
            $this->db->runQuery(
                "UPDATE `".$this->tb_product."` SET 
                            `spec_group_id` = ? 
                        WHERE `id` = ? LIMIT 1 ",
                [ 'd', 'd' ], [ $new_group_id, $product_id ]
            );

            $this->updateSpecGroupProductCount($new_group_id);

            $this->clearProductAttributes($product_id);

            return true;
        }

        return false;
    }


    protected function updateSpecGroupProductCount($group_id) {
        $this->db->runQuery(
            "UPDATE `".$this->tb_product_spec_group."` SET 
                        `product_count` = (SELECT COUNT(*) FROM ".$this->tb_product." WHERE `spec_group_id` = ? )
                    WHERE `id` = ? LIMIT 1 ",
            [ 'd', 'd' ], [ $group_id, $group_id ]
        );
    }


    public function getProductSpecGroupInfo($product_id)
    {
        $product_spec_group_id = $this->getProductSpecGroupId($product_id);

        return ($product_spec_group_id) ? $this->getSpecGroupInfo($product_spec_group_id) : false ;
    }


    public function getProductSpecGroupId($product_id)
    {
        $query = $this->db->runQuery("SELECT `spec_group_id` FROM `".$this->tb_product."` WHERE `id` = ? LIMIT 1  ", ['d'], [$product_id]);

        if ($info = $this->db->fetchAssoc($query) ) {
            return $info['spec_group_id'] ;
        }

        return 0;
    }


    public function getSpecGroupInfo($group_id)
    {
        $query = $this->db->runQuery("SELECT * FROM `".$this->tb_product_spec_group."` WHERE `id` = ? ", ['d'], [$group_id]);

        return $this->db->fetchAssoc($query);
    }


    protected function buildQueryCondition(array $condition)
    {
        $catCondition = [];

        //Tim kiem theo tu khoa
        if(isset($condition["q"]) && $condition["q"]){
            $catCondition[] = " AND `title` LIKE '%".$this->db->escape($condition["q"])."%' ";
        }

        return join(" ", $catCondition);
    }

    protected function _buildQueryConditionExtend(array $filter_condition) : ?array
    {
        $where_condition = "";
        $bind_types = [];
        $bind_values = [];

        return [
            $where_condition,
            $bind_types,
            $bind_values
        ];
    }


}
