<?php

namespace App\Models\Cloudflare\API;

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

class FirewallModel extends Model
{
    const PARENT_FIELD = "zone_uid";
    protected $DBGroup          = 'default';
    protected $table            = 'cloudflarefirewall';
    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', 'zone_uid', 'description', 'filter_id', 'filter_expression', 'filter_paused', 'paused', 'action', '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]',
        'zone_uid'          => 'if_exist|min_length[10]|max_length[200]',
        'description'       => 'if_exist|string',
        'filter_id'         => 'if_exist|min_length[10]|max_length[200]',
        'filter_expression' => 'if_exist|string',
        'filter_paused'     => 'if_exist|in_list[on,off]',
        'paused'            => 'if_exist|in_list[on,off]',
        'action'            => 'if_exist|string',
        'updated_at'        => 'if_exist|valid_date',
        'created_at'        => 'if_exist|valid_date',
    ];
    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|FirewallEntity
    {
        $entity = $this->asObject(FirewallEntity::class)->where('uid', $uid)->first();
        if (is_null($entity)) {
            throw new \Exception(__METHOD__ . "에서 {$uid} 해당 정보가 없습니다.");
        }
        return $entity;
    }

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