<?php

namespace Hura8\Components\Customer\Model;

use Hura8\Interfaces\AppResponse;
use Hura8\Interfaces\iSearch;
use Hura8\System\IDGenerator;
use Hura8\System\Model\aEntityBaseModel;
use Hura8\Interfaces\iEntityModel;
use Hura8\Interfaces\EntityType;
use Hura8\System\Security\DataValidator;


class CustomerModel extends aEntityBaseModel implements iEntityModel
{

    protected $user_types = [
        "auto"      => "Chưa đăng ký",
        "register"  => "Đăng ký thành viên",
    ];

    /* @var iSearch $objSearchModel */
    protected  $objSearchModel;

    public function __construct() {
        $this->objSearchModel = new CustomerSearchModel();

        parent::__construct(
            EntityType::CUSTOMER,
            "",
            $this->objSearchModel
        );
    }


    protected function extendedFilterOptions() : array
    {
        return [
            'province' => 0,
            'user_type' => '',
        ];
    }


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

        return false;
    }


    public function getInfoByEmail($email, $user_type='register')
    {
        $query = $this->db->runQuery(
            "SELECT * FROM `".$this->tb_entity."` WHERE `email` = ? AND `type` = ? LIMIT 1 ",
            ['s', 's'], [$email, $user_type]
        ) ;

        if( $item_info = $this->db->fetchAssoc($query)){
            return $this->formatItemInfo($item_info);
        }

        return null;
    }


    public function getInfoByVerifiedEmail($email)
    {
        $info = $this->getInfoByEmail($email);
        if($info && $info['is_email_verify']) {
            return $info;
        }

        return null;
    }


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

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


        if(isset($filter_condition["province"]) && $filter_condition["province"]) {
            $catCondition[] = " AND `province` = ?  ";
            $bind_types[] = 'd';
            $bind_values[] = $filter_condition["province"];
        }

        // user_type
        if(isset($filter_condition["user_type"]) && array_key_exists($filter_condition["user_type"], $this->user_types) ){
            $catCondition[] = " AND `type` = ?  ";
            $bind_types[] = 's';
            $bind_values[] = $filter_condition["user_type"];
        }


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


    protected function formatItemInfo(array $item_info): array
    {
        if($item_info["birth_day"] && $item_info["birth_year"]) {
            $item_info["birth_day"] = $item_info["birth_day"]."-".$item_info["birth_year"];
        }

        return $item_info;
    }


    protected function createWebCRMCode($input_code = '') {
        return $input_code ?: "Web-".IDGenerator::createStringId(8);
    }


}
