<?php

namespace Hura8\System\Model;

use Hura8\Interfaces\AppResponse;
use Hura8\Interfaces\EntityType;

class DomainModel extends aEntityBaseModel
{

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

    protected function extendedFilterOptions(): array
    {
        return [

        ];
    }


    public function addNewDomain($clean_domain, $language = DEFAULT_LANGUAGE){
        return $this->create([
            'domain' => $clean_domain,
            'lang' => $language,
        ]);
    }


    protected function getInfoByDomain($clean_domain){
        $query = $this->db->runQuery(
            "SELECT * from ".$this->tb_entity." WHERE  `domain` = ? LIMIT 1 ",
            ['s'], [ $clean_domain ]
        );

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


    public function deleteDomain($clean_domain){
        $domain_info = $this->getInfoByDomain($clean_domain);
        if($domain_info) {
            $this->delete($domain_info['id']);
        }

        return true;
    }


    public function setDomainMain($domain, $language){

        $this->db->update(
            $this->tb_entity,
            [ "is_main" => 0, ],
            [
                "lang" => $language,
                "is_main" => 1
            ]
        );

        $this->db->update(
            $this->tb_entity,
            [
                "is_main" => 1,
                "last_update" => CURRENT_TIME,
                "last_update_by" => ADMIN_NAME,
            ],
            [
                "domain" => $domain,
            ]
        );

        return true;
    }


    public function setDomainLayout($domain, $layout = 'pc'){
        $this->db->update(
            $this->tb_entity,
            [ "layout" => $layout, ],
            [ "domain" => $domain, ]
        );

        return true;
    }


    protected function _buildQueryConditionExtend(array $condition) : ?array
    {
        $where_condition = "";
        $bind_types = [];
        $bind_values = [];

        if(isset($condition['language']) && $condition['language']) {
            $where_condition = " AND `lang` = ? ";
            $bind_types[] = 's';
            $bind_values[] = $condition['language'];
        }

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


    protected function beforeCreateItem(array $input_info) : AppResponse
    {
        $info = $input_info;

        // if domain exist
        if($this->getInfoByDomain($info['domain'])) {
            return new AppResponse('error', "Domain exist");
        }

        $info['create_time'] = CURRENT_TIME;
        $info['create_by'] = ADMIN_NAME;

        return new AppResponse('ok', null, $info);
    }

    protected function afterCreateItem($new_item_id, $new_item_info)
    {
        // TODO: Implement afterCreateItem() method.
    }

    protected function beforeUpdateItem($item_id, $current_item_info, $new_input_info) : AppResponse
    {
        $info = $new_input_info;

        $info['last_update'] = CURRENT_TIME;
        $info['last_update_by'] = ADMIN_NAME;

        return new AppResponse('ok', null, $info);
    }

    protected function afterUpdateItem($item_id, $old_item_info, $new_item_info)
    {
        // TODO: Implement afterUpdateItem() method.
    }

    protected function beforeDeleteItem($item_id, $item_info) : AppResponse
    {
        return new AppResponse('ok');
    }

    protected function afterDeleteItem($item_id, $item_info)
    {
        // TODO: Implement afterDeleteItem() method.
    }
}
