<?php

namespace Hura8\Components\ComboSet\Model;

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


class ComboSetModel extends aEntityBaseModel implements iEntityModel
{

    protected $tb_set_product   = 'tb_combo_set_product';

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


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


    public function getAllSetIdsForAProduct($product_id)
    {
        $query = $this->db->runQuery(
            " SELECT `set_id`  FROM ".$this->tb_set_product."   WHERE `product_id` = ? ",
            ['d'], [$product_id]
        );

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

        return $item_list;
    }


    public function getTotalProductUseSet($set_id)
    {
        // search
        $keyword = getRequest("q");
        if($keyword) {
            $search         = new ProductSearchModel();
            $match_result   = $search->find($keyword);
            $catCondition   = (sizeof($match_result) > 0) ? " AND `product_id` IN (".join(",", $match_result).") " : " AND `product_id` = -1 ";

            $query = $this->db->runQuery("
                            SELECT COUNT(product_id) AS total_product
                            FROM ".$this->tb_set_product." 
                            WHERE `set_id` = ? " . $catCondition ."
                        ", ['d'], [$set_id]);

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

            return 0;

        } else {
            $set_info = $this->getInfo($set_id);

            return $set_info['product_count'];
        }
    }


    public function getListProductUseSet($set_id, $numPerPage)
    {
        $page = getPageId();

        // search
        $catCondition = "";
        $keyword = getRequest("q");
        if($keyword) {
            $search         = new ProductSearchModel();
            $match_result   = $search->find($keyword);
            $catCondition   = (sizeof($match_result) > 0) ? " AND `product_id` IN (".join(",", $match_result).") " : " AND `product_id` = -1 ";
        }

        $query = $this->db->runQuery("
                            SELECT `product_id`
                            FROM ".$this->tb_set_product." 
                            WHERE `set_id` = ? " . $catCondition ."
                            ORDER BY id desc
                            LIMIT ".($page - 1) * $numPerPage .", ".$numPerPage." 
                        ", ['d'], [$set_id]);

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

        return $item_list;
    }


    protected function _buildQueryOrderBy(string $sort_by = "new")
    {
        $order_condition = "";

        switch ($sort_by) {
            case "ordering";
                $order_condition = "  `ordering` desc ";
                break;
            case "old";
                $order_condition = "  id asc ";
                break;
            case "last_show_time";
                $order_condition = "  last_show_time ASC ";
                break;
        }

        return $order_condition;
    }


    protected function formatItemInfo(array $item_info) : array
    {
        $from_time = $item_info['from_time'];
        $from_time_date = ($from_time > 0) ? date("d-m-Y", $from_time) : '';
        $from_time_minute = ($from_time > 0) ? date("H:i", $from_time) : "00:00";

        $to_time = $item_info['to_time'];
        $to_time_date = ($to_time > 0) ? date("d-m-Y", $to_time) : '';
        $to_time_minute = ($to_time > 0) ? date("H:i", $to_time) : "00:00";

        $item_info['from_time_date'] = $from_time_date;
        $item_info['from_time_minute'] = $from_time_minute;
        $item_info['to_time_date'] = $to_time_date;
        $item_info['to_time_minute'] = $to_time_minute;

        return $item_info;
    }



    ///---------
    ///
    protected function _buildQueryConditionExtend(array $filter_condition) : ?array
    {

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

        if(isset($filter_condition["product_id"]) && $filter_condition["product_id"]){
            $catCondition .= " AND `id` IN ( SELECT `set_id` FROM ".$this->tb_set_product." WHERE `product_id` = ? ) ";

            $bind_types[] = 'd';
            $bind_values[] = $filter_condition['product_id'];
        }

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


}
