<?php

namespace App\Libraries\Cloudflare\API\Magictransit;

use App\Entities\Cloudflare\AllowListEntity;

class AllowList extends \App\Libraries\Cloudflare\API\API
{
    private $_entity = null;
    public function __construct()
    {
        parent::__construct();
        $this->_model = new \App\Models\Cloudflare\Magictransit\AllowListModel();
    }
    public function setParentAuthEntity($parent_uid): void
    {
        $parent_entity = $this->getAccountModel()->getEntity($parent_uid);
        $this->setParent($parent_entity);
        $auth_entity = $this->getAuthModel()->getEntity($parent_entity->getParentFieldData());
        $this->setAuth($auth_entity);
    }
    public function getClassName()
    {
        return 'AllowList';
    }
    protected function getEndPoint()
    {
        return is_null($this->_endPoint) ? "client/v4/accounts/{$this->getParent()->getPrimaryKey()}5/magic/advanced_tcp_protection/configs/allowlist" : $this->_endPoint;
    }
    public function getEntityByResult(\stdClass $cfResult): AllowListEntity
    {
        $entity = is_null($this->_entity) ? new AllowListEntity() : $this->_entity;
        $entity->uid = $cfResult->id;
        $entity->zone_uid = $this->getParent()->getPrimaryKey();
        $entity->description = $cfResult->description;
        $entity->filter_id = $cfResult->filter->id;
        $entity->filter_expression = isset($cfResult->filter->expression) && $cfResult->filter->expression ? $cfResult->filter->expression : ' ';
        $entity->filter_paused = isset($cfResult->filter->paused) && $cfResult->filter->paused ? 'on' : 'off';
        $entity->action = $cfResult->action;
        $entity->paused = $cfResult->paused ? 'off' : 'on';
        $entity->updated_at = $cfResult->modified_on;
        $entity->created_at = $cfResult->created_on;
        // parent::add_logs("notice","host:[{$cfResult->name}<>{$entity->description}] | proxied:[{$cfResult->proxied}<>{$entity->proxied}] | locked:[{$cfResult->locked}<>{$entity->locked}]");
        return $entity;
    }

    // public function insert(){ }
    public function update(AllowListEntity $entity, array $fieldDatas): AllowListEntity
    {
        $this->setParentAuthEntity($entity->getParentFieldData());

        $isChanged = false;
        foreach ($fieldDatas as $field => $value) {
            if ($entity->$field != $value) {
                $entity->$field = $value;
                $isChanged = true;
            }
        }
        if (!$isChanged) {
            return $entity;
        }
        $rule = array_merge(
            [
                'id' => $entity->getPrimaryKey(),
                'filter' => [
                    'id' => $entity->filter_id,
                    'expression' => $entity->filter_expression,
                    'paused' => isset($entity->filter_paused) && $entity->filter_paused === 'on' ? true : false
                ]
            ],
            [
                'paused' => isset($entity->paused) && $entity->paused === 'on' ? false : true,
                'action' => $entity->action
            ]
        );
        if (!is_null($entity->description)) {
            $rule['description'] = $entity->description;
        }

        $cfResult = $this->getAdapter()->put('zones/' . $this->getParent()->getPrimaryKey() . '/AllowList/rules/' . $entity->getPrimaryKey(), $rule);
        $cfResult = json_decode($cfResult->getBody());
        if (!$cfResult->success) {
            throw new \Exception(var_export($cfResult, true));
        }
        return $this->getEntityByResult($cfResult->result);
    }
    // public function delete(){ }
    public function sync(AllowListEntity $entity): AllowListEntity
    {
        $this->setParentAuthEntity($entity->getParentFieldData());

        $cfResult = $this->getAdapter()->get('zones/' . $this->getParent()->getPrimaryKey() . '/AllowList/rules/' . $entity->getPrimaryKey());
        $cfResult = json_decode($cfResult->getBody());
        if (!$cfResult->success) {
            throw new \Exception(var_export($cfResult, true));
        }
        return $this->getEntityByResult($cfResult->result);
    }
    protected function getCFResults_List(int $page): array
    {
        return $this->getEndPoint()->listAllowListRules($this->getParent()->getPrimaryKey(), $page, CF_ADAPTER_PERPAGE_MAX)->result;
    }
}
