<?php

namespace App\Models\Cloudflare\API;

use App\Entities\Cloudflare\API\ZoneEntity;
use CodeIgniter\Model;

class ZoneModel extends Model
{
    const PARENT_FIELD = "account_uid";
    protected $DBGroup          = 'default';
    protected $table            = 'cloudflarezone';
    protected $primaryKey       = 'uid';
    protected $useAutoIncrement = false;
    protected $insertID         = 0;
    protected $returnType       = 'array'; //object,array,entity명::class
    protected $useSoftDeletes   = false;
    protected $protectFields    = true;
    protected $allowedFields    = ['uid', 'account_uid', 'uid', 'domain', 'name_servers', 'original_name_servers', 'plan', 'development_mode', 'ipv6', 'security_level', 'status', 'updated_at', 'crated_at'];

    // Dates
    protected $useTimestamps = true;
    protected $dateFormat    = 'datetime';
    protected $createdField  = 'created_at';
    protected $updatedField  = 'updated_at';
    protected $deletedField  = 'deleted_at';

    // Validation
    protected $validationRules      = [
        'uid'               => 'if_exist|min_length[10]|max_length[200]',
        'account_uid'       => 'if_exist|min_length[10]|max_length[200]',
        'domain'            => 'if_exist|string',
        'name_servers'      => 'if_exist|string',
        'plan'              => 'if_exist|string',
        'development_mode'  => 'if_exist|in_list[on,off]',
        'ipv6'              => 'if_exist|in_list[on,off]',
        'security_level'    => 'if_exist|string',
        'status'            => 'if_exist|string',
        'updated_at'        => 'if_exist|valid_date',
        'created_at'        => 'if_exist|valid_date',
    ];
    // 'security_level'    => 'if_exist|in_list[essentially_off,low,medium,under_attack]',
    // 'status'            => 'if_exist|in_list[active,pending]',
    protected $validationMessages   = [];
    protected $skipValidation       = true;
    protected $cleanValidationRules = true;

    // Callbacks
    protected $allowCallbacks = true;
    protected $beforeInsert   = [];
    protected $afterInsert    = [];
    protected $beforeUpdate   = [];
    protected $afterUpdate    = [];
    protected $beforeFind     = [];
    protected $afterFind      = [];
    protected $beforeDelete   = [];
    protected $afterDelete    = [];

    public function getTableName()
    {
        return $this->table;
    }

    public function getEntity(string $uid): null|ZoneEntity
    {
        $entity = $this->asObject(ZoneEntity::class)->where('uid', $uid)->first();
        if (is_null($entity)) {
            throw new \Exception(__METHOD__ . "에서 {$uid} 해당 정보가 없습니다.");
        }
        return $entity;
    }

    //Index 검색용
    public function setIndexWordFilter(string $word)
    {
        $this->like('domain', $word, 'both'); //befor , after , both
    }
    public function setIndexDateFilter($start, $end)
    {
        $this->where('created_at >=', $start);
        $this->where('created_at <=', $end);
    }
    public function setIndexOrderBy($field, $order = 'ASC')
    {
        $this->orderBy("domain ASC, {$field} {$order}");
    }

    //도메인이 이미 존재하는지 체크
    public function isUniqueDomain(string $account_uid, string $domain): bool
    {
        $this->where('account_uid', $account_uid);
        $this->where('domain', $domain);
        return is_null($this->first()) ? true : false;
    }
}
