<?php

namespace Hura8\Components\Template\Model;

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

class TemplateModel extends aEntityBaseModel
{

    protected $template_set = '';

    protected $tb_template = '';
    protected $tb_template_history = 'tb_template_history';


    public function __construct($template_set='default')
    {
        parent::__construct('template');
        $this->template_set = $template_set;
        $this->tb_template = $this->tb_entity;
    }


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


    public function getModuleList() {
        $query = $this->db->runQuery(
            "  select distinct `module` from `".$this->tb_template."` where `set_name` = ?  order by `module` asc  ",
            [ 's' ],
            [ $this->template_set ]
        ) ;

        $item_list = [];
        foreach ($this->db->fetchAll($query) as $item) {
            if($item['module']) $item_list[] = $item['module'];
        }

        return $item_list;
    }


    public function checkFileExist($file_name, $f_type ) {
        $query = $this->db->runQuery(
            "SELECT *  FROM ".$this->tb_template." WHERE `file_name` = ? AND `file_type` = ? AND `set_name` = ?  LIMIT 1 ",
            [ 's', 's' , 's' ],
            [ $file_name, $f_type, $this->template_set ]
        );

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


    public function getVersionHistory($module, $tpl_file) {

        if(!$tpl_file) return [];

        $history_tpl_file = $this->historyFile($module, $tpl_file);

        $query = $this->db->runQuery(
            "SELECT tpl_version, last_update, last_update_by  FROM ".$this->tb_template_history." 
             WHERE `template` = ? AND `set_name` = ?  ORDER BY `tpl_version` DESC  LIMIT 30 ",
            [ 's', 's' ],
            [ $history_tpl_file, $this->template_set ]
        );

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


    public function getVersionContent($module, $tpl_file, $version) {

        $history_tpl_file = $this->historyFile($module, $tpl_file);

        $query = $this->db->runQuery(
            "SELECT `content`  FROM ".$this->tb_template_history." 
            WHERE `template`= ? AND `set_name` = ? AND `tpl_version` = ?
            LIMIT 1 ",
            [ 's', 's', 'd' ],
            [ $history_tpl_file, $this->template_set, $version ]
        );

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

        return '';
    }

    protected function historyFile($module, $tpl_file) {
        return join("/", [$module, $tpl_file]);
    }


    protected function _buildQueryConditionExtend(array $conditions) : ?array
    {

        /*$conditions = [
            'file_folder' => '',
            'not_image' => true,
        ];*/

        $where_clause = [ " AND `set_name` = ?  "];
        $bind_types = ['s'];
        $bind_values = [$this->template_set];

        if(isset($conditions['file_folder']) && in_array($conditions['file_folder'], ['layout', 'images', 'script']) ) {
            $where_clause[] =  " AND `file_folder` = ?  ";
            $bind_types[] = 's';
            $bind_values[] = $conditions['file_folder'];
        }

        if(isset($conditions['not_image']) && $conditions['not_image']) {
            $where_clause[] =  " AND `file_folder` != 'images'  ";
        }

        return [
            join(" ", $where_clause),
            $bind_types,
            $bind_values
        ];
    }


}
