<?php

namespace App\Controllers\Admin\Cloudflare;

use App\Libraries\MySocket\Cloudflare\AccountSocket;
use App\Libraries\MySocket\Cloudflare\ZoneSocket;
use App\Models\Cloudflare\AccountModel;
use CodeIgniter\HTTP\DownloadResponse;
use CodeIgniter\HTTP\RedirectResponse;
use CodeIgniter\HTTP\RequestInterface;
use CodeIgniter\HTTP\ResponseInterface;
use Psr\Log\LoggerInterface;

class AccountController extends CloudflareController
{
    private $_mySocket = null;
    private $_model = null;
    private $_auth_key = null;
    public function initController(RequestInterface $request, ResponseInterface $response, LoggerInterface $logger)
    {
        parent::initController($request, $response, $logger);
        $this->class_name = "Account";
        $this->class_path = $this->root . $this->class_name;
        $this->title      = lang("{$this->class_path}.title");
        helper($this->class_path);
    }
    final protected function getModel(): AccountModel
    {
        if ($this->_model === null) {
            $this->_model = new AccountModel();
        }
        return $this->_model;
    }
    final protected function getMySocket(): AccountSocket
    {
        if ($this->_mySocket === null) {
            $this->_mySocket = new AccountSocket($this->_auth_key);
        }
        return $this->_mySocket;
    }
    protected function getFormFieldOption(string $field, array $options = []): array
    {
        switch ($field) {
            case $this->getModel()::PARENT:
                // $this->getAuthModel()->where('status', DEFAULTS['STATUS']);
                $options[$field] = $this->getAuthModel()->getFormFieldOption($field);
                // echo $this->getAuthModel()->getLastQuery();
                // dd($options);
                break;
            default:
                $options = parent::getFormFieldOption($field, $options);
                break;
        }
        return $options;
    }
    //  리스트
    public function index(): string
    {
        $this->action          = __FUNCTION__;
        $this->fields          = [$this->getModel()::PARENT, $this->getModel()::TITLE, 'type', 'status', 'updated_at', 'created_at'];
        $this->field_rules     = $this->getModel()->getFieldRules($this->action, $this->fields);
        $this->filter_fields   = [$this->getModel()::PARENT, 'type', 'status'];
        $this->field_options   = $this->getFormFieldOptions($this->filter_fields);
        $this->batchjob_fields = ['typep', 'status'];
        return $this->list_procedure();
    }
    // Download
    public function download(string $output_type, $uid = false): DownloadResponse|string
    {
        $this->action          = __FUNCTION__;
        $this->fields          = [$this->getModel()::PARENT, $this->getModel()::TITLE, 'type', 'status', 'updated_at', 'created_at'];
        $this->field_rules     = $this->getModel()->getFieldRules($this->action, $this->fields);
        $this->filter_fields   = [$this->getModel()::PARENT, 'type', 'status'];
        $this->field_options   = $this->getFormFieldOptions($this->filter_fields);
        $this->batchjob_fields = ['typep', 'status'];
        return $this->download_procedure($output_type, $uid);
    }
    //Zone Reload By Account
    protected function reload_process(mixed $uid = false): void
    {
        if ($uid) {
            $this->getModel()->where($this->getModel()::PK, $uid);
        }
        foreach ($this->getModel()->getEntitys() as $entity) {
            $zone_socket = new ZoneSocket($entity);
            $zone_socket->reload();
        }
    }
    public function reload(mixed $uid = false): RedirectResponse
    {
        return $this->reload_procedure($uid);
    }
}
