<?php

namespace App\Controllers\Admin\Cloudflare;

use App\Helpers\Cloudflare\AccountHelper;
use App\Libraries\Cloudflare\Account;
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 $_myLibrary = null;
    private $_model = null;
    private $_auth_entity = null;
    public function initController(RequestInterface $request, ResponseInterface $response, LoggerInterface $logger)
    {
        parent::initController($request, $response, $logger);
        $this->class_name = "Account";
        $this->class_path .= $this->class_name;
        $this->title      = lang("{$this->class_path}.title");
        $this->helper     = new AccountHelper();
    }
    final protected function getModel(): AccountModel
    {
        if ($this->_model === null) {
            $this->_model = new AccountModel();
        }
        return $this->_model;
    }
    final protected function getMyLibrary(): Account
    {
        if ($this->_myLibrary === null) {
            $this->_myLibrary = new Account($this->_auth_entity);
        }
        return $this->_myLibrary;
    }
    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 = [];
        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 = ['type', 'status'];
        return $this->download_procedure($output_type, $uid);
    }
    //reload Account By Auth
    protected function reload_process(mixed $uid): void
    {
        $this->_auth_entity = $this->getAuthModel()->getEntityByPK(intval($uid));
        if ($this->_auth_entity === null) {
            throw new \Exception("Auth: {$uid} 정보를 찾을수 없습니다.");
        }
        $this->getModel()->where($this->getModel()::PARENT, $this->_auth_entity->getPK());
        foreach ($this->getModel()->getEntitys() as $entity) {
            $this->getMyLibrary()->reload();
        }
    }
    public function reload(int $uid): RedirectResponse
    {
        return $this->reload_procedure($uid);
    }
}
