<?php

namespace Hura8\Components\Product\Model;

use Hura8\Interfaces\AppResponse;
use Hura8\System\Language;
use Hura8\System\Model\aEntityBaseModel;
use Hura8\Interfaces\iEntityModel;
use Hura8\Interfaces\EntityType;
use Hura8\System\Security\DataClean;
use Hura8\System\Security\DataType;

class ProductAttributeModel extends aEntityBaseModel {

    protected $tb_product_category          = "tb_product_category";
    protected $tb_attribute_value           = "tb_attribute_value";

    protected $tb_product_spec_group        = "tb_product_spec_group";
    protected $tb_attribute_per_spec_group  = "tb_attribute_per_spec_group";
    protected $tb_attribute_per_category    = "tb_attribute_per_category";

    public function __construct() {
        parent::__construct(EntityType::PRODUCT_ATTRIBUTE);
    }


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

    // SPEC-GROUP

    public function getSpecGroupAttributeWithValues($group_id) {

        $query = $this->db->runQuery(
            "SELECT 
                           a.title,
                           a.summary,
                           a.is_filter,
                           a.value_match_all,
                           a.filter_code,
                           a.is_display,
                           a.is_header, 
                           a.is_multi, a.in_summary,
                           a.value_count,
                           g.attr_id,
                           g.ordering, 
                           g.status 
                    FROM ".$this->tb_attribute_per_spec_group." g, ".$this->tb_entity." a
                   WHERE g.`group_id`= ? AND g.`attr_id`= a.`id`
                   ORDER BY g.`ordering` DESC, a.`ordering` DESC ",
            ['d'],
            [$group_id]
        ) ;

        $attribute_ids = [];
        $value_per_attribute_ids = [];
        $result = [];
        foreach ($this->db->fetchAll($query) as $item) {
            $attribute_ids[] = $item['attr_id'];
            $value_per_attribute_ids[$item['attr_id']] = [];

            $result[$item['attr_id']] = [
                "attribute_info" => $item,
                "attribute_values" => &$value_per_attribute_ids[$item['attr_id']],
            ];
        }


        if(!sizeof($attribute_ids)) {
            return [];
        }

        // now get all values for each attribute
        list($parameterized_ids, $bind_types) = create_bind_sql_parameter_from_value_list($attribute_ids, "int");

        $query = $this->db->runQuery(
            "SELECT * FROM ".$this->tb_attribute_value."
                   WHERE `attribute_id` IN (".$parameterized_ids.")
                   ORDER BY `ordering` DESC, `title` ASC ",
            $bind_types,
            $attribute_ids
        ) ;

        foreach ($this->db->fetchAll($query) as $item) {
            $value_per_attribute_ids[$item['attribute_id']][]  = $item;
        }

        return $result;
    }


    public function getSpecGroupAttribute($group_id) {

        $query = $this->db->runQuery(
            "SELECT 
                           a.title,
                           a.summary,
                           a.is_filter,
                           a.value_match_all,
                           a.filter_code,
                           a.is_display,
                           a.is_header, a.is_multi, a.value_count,
                           g.attr_id,
                           g.ordering, 
                           g.status 
                    FROM ".$this->tb_attribute_per_spec_group." g, ".$this->tb_entity." a
                   WHERE g.`group_id`= ? AND g.`attr_id`= a.`id`
                   ORDER BY g.`ordering` DESC, a.`ordering` DESC ",
            ['d'],
            [$group_id]
        ) ;

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


    public function getProductMatchAllAttributeValueIds(array $_att_value_list) {
        /*$query = $this->db->runQuery("
										SELECT `pro_id` , COUNT(*) AS num_pro
										FROM ".TB_PRODUCT_ATTRIBUTE."
										WHERE " . join(" OR ", array_map(function ($_item){ return " `attr_value_id` = '".intval($_item['id'])."' "; }, $_att_value_list )) ."
										GROUP BY pro_id
										HAVING num_pro = ".sizeof($_att_value_list)."
										LIMIT 10000
									");

        return array_map(function ($item){
            return $item["pro_id"];
        }, $this->db->fetchAll($query));*/

        return [];
    }

    public function getProductMatchAttributeValue(array $value_ids) {
        $product_filter_id_match = array();
        // todo:
        /*if(sizeof($query_attr_id)) {
            $query = $this->db->runQuery("
										SELECT DISTINCT pro_id , COUNT(*) as num_pro
										FROM ".TB_PRODUCT_ATTRIBUTE."
										WHERE attr_value_id IN (".join(',', $query_attr_id).")
										GROUP BY pro_id
										HAVING num_pro = ".$count_filter."
										LIMIT 10000
									");
            foreach ( $this->db->fetchAll($query) as $rs ) {
                $product_filter_id_match[] = $rs["pro_id"];
            }
        }*/

        return $product_filter_id_match;
    }

    protected function _buildQueryConditionExtend(array $filter_condition) : ?array
    {
        /*$condition = array(
            "q" => "",
            "letter" => "",
            "status" => 0,
        );*/

        $catCondition = [];
        $bind_types = [];
        $bind_values = [];

        return array( join(" ", $catCondition), $bind_types, $bind_values);
    }



}
