<?php

namespace Hura8\Components\Marketing\Model;

use Hura8\Components\Product\AdminController\AProductController;
use Hura8\Interfaces\AppResponse;
use Hura8\System\IDGenerator;
use Hura8\System\Model\aEntityBaseModel;

class ProductFeedModel extends aEntityBaseModel
{

    protected $tb_feed_product  = "tb_feed_product";

    public function __construct() {
        parent::__construct('feed');

        //$this->all_brands       = $this->buildAllBrands();
        //$this->all_categories   = $this->buildAllCategories();
    }


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


    public function getInfoByPublicId($public_id)
    {
        $query = $this->db->runQuery("SELECT * FROM `".$this->tb_entity."` WHERE `public_id` = ? LIMIT 1 ", ['s'], [$public_id]) ;
        if( $item_info = $this->db->fetchAssoc($query)){
            return $this->formatItemInfo($item_info);
        }

        return false;
    }

    public function getProductListTotal($list_id) {
        $query = $this->db->runQuery(
            " SELECT COUNT(*) AS total FROM `".$this->tb_feed_product."` WHERE `list_id` = ? ",
            ['d'], [$list_id]
        );

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

        return $total;
    }

    public function getProductList($list_id, $page = 1, $numPerPage = 30) {

        $query = $this->db->runQuery(
            " SELECT `pro_id` FROM `".$this->tb_feed_product."` 
                    WHERE `list_id` = ? 
                    ORDER BY `id` DESC 
                    LIMIT ".( ($page - 1) * $numPerPage ).", ".$numPerPage,
            ['d'], [$list_id]
        );

        $product_list_ids = array_map(function ($item) { return $item['pro_id'];}, $this->db->fetchAll($query));

        $objAProductController = new AProductController();

        $stt = ($page - 1) * $numPerPage;
        $item_list = [];
        foreach ($objAProductController->getListByIds($product_list_ids) as $_id => $info) {
            $stt++;
            $info["counter"] = $stt;
            $item_list[] = $info;
        }

        return $item_list;
    }

    //--------------------

    public function getAllCategories() {
        return $this->all_categories;
    }


    public function getAllProductListIds( $list_id){
        if(!$list_id) return [];

        $query = $this->db->runQuery("SELECT `pro_id` FROM `".$this->tb_feed_product."` WHERE `list_id` = ? ", ['d'], [ $list_id ]);

        $item_list = array();
        foreach ( $this->db->fetchAll($query) as $rs ) {
            $item_list[] = $rs['pro_id'];
        }

        return $item_list;
    }


    //--------------------


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

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


        if(isset($filter_condition["letter"]) && strlen($filter_condition["letter"]) == 1){
            $catCondition[] = " AND `letter` = ?  ";
            $bind_types[] = 's';
            $bind_values[] = $filter_condition["letter"];
        }

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



}
