<?php

namespace Hura8\Components\Product\Model;

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

class ProductImageModel extends aEntityBaseModel
{

    protected $product_id = 0;

    /* @var  ProductModel $objProductModel */
    protected  $objProductModel;

    public function __construct($product_id = 0) {
        parent::__construct('product_image');

        $this->product_id = $product_id;
        $this->objProductModel = new ProductModel();
    }


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


    public function setProduct($product_id) {
        $this->product_id = $product_id;
    }

    public function countProductImage($product_id) {
        $query = $this->db->runQuery("SELECT COUNT(*) AS total FROM `".$this->tb_entity."` WHERE `pro_id` = ? ", ['d'], [ $product_id]);
        if($info = $this->db->fetchAssoc($query)) {
            return $info['total'];
        }

        return 0;
    }


    protected function _buildQueryOrderBy($sort_by = "new")
    {
        return " `ordering` DESC, `id` DESC ";
    }


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

        $catCondition = [" AND `pro_id` = ? "];
        $bind_types = ['d'];
        $bind_values = [$this->product_id];

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


    protected function getProductMainImage() {
        $query = $this->db->runQuery(
            "SELECT `id` FROM `".$this->tb_entity."` WHERE `pro_id` = ?  AND `is_main` = 1 LIMIT 1",
            ['d'], [ $this->product_id]
        );

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

}
