<?php

namespace App\Services;

use App\Entities\FormOptionEntity;
use App\Libraries\LogCollector;
use CodeIgniter\Model;

abstract class CommonService
{
    private $_model = null;
    private $_classNames = [];
    private $_serviceDatas = [];
    protected function __construct(Model $model)
    {
        $this->_model = $model;
    }
    abstract public function getFormFields(): array;
    abstract public function getIndexFields(): array;
    //기본 기능부분
    final public function __get($name)
    {
        if (!array_key_exists($name, $this->_serviceDatas)) {
            return null;
        }
        return $this->_serviceDatas[$name];
    }
    final public function __set($name, $value): void
    {
        $this->_serviceDatas[$name] = $value;
    }
    final protected function addClassName(string $className): void
    {
        $this->_classNames[] = $className;
    }
    final public function getClassName($delimeter = DIRECTORY_SEPARATOR): string
    {
        return implode($delimeter, $this->_classNames);
    }
    final protected function getModel(): mixed
    {
        if (!$this->_model) {
            throw new \Exception("Model이 정의되지 않았습니다. " . __METHOD__);
        }
        return $this->_model;
    }
    final public function getEntity(mixed $where, ?string $message = null): mixed
    {
        try {
            $entity = is_array($where) ? $this->getModel()->where($where)->first() : $this->getModel()->find($where);
            if (!$entity) {
                throw new \Exception($message ?? __METHOD__ . "에서 해당 정보가 존재하지 않습니다");
            }
            return $this->getEntity_process($entity);
        } catch (\Exception $e) {
            $message = sprintf(
                "\n------%s SQL오류-----\n%s\n------------------------------\n",
                __FUNCTION__,
                $this->getModel()->getLastQuery()
            );
            throw new \Exception($message);
        }
    }
    final public function getEntities(mixed $where = null, array $columns = ['*']): array
    {
        try {
            $entities = $this->getEntities_process($where, $columns);
            $debug = sprintf("debug.%s.%s", $this->getClassName(), __FUNCTION__);
            if (env($debug, false)) {
                echo $debug . "=>" . $this->getModel()->getLastQuery() . "<BR>";
            }
            return $entities;
        } catch (\Exception $e) {
            $message = sprintf(
                "\n------%s SQL오류-----\n%s\n------------------------------\n",
                __FUNCTION__,
                $this->getModel()->getLastQuery()
            );
            throw new \Exception($message);
        }
    } //
    //삭제
    final public function delete(mixed $entity): mixed
    {
        $result = $this->getModel()->delete($entity->getPK());
        if (!$result) {
            $message = sprintf(
                "\n------%s SQL오류-----\n%s\n------------------------------\n",
                __FUNCTION__,
                $this->getModel()->getLastQuery()
            );
            LogCollector::error($message);
            throw new \Exception($message);
        }
        LogCollector::info("[{$entity->getTitle()}]" . MESSAGES["DELETED"] . ":");
        return $entity;
    }
    //Index용
    final public function getTotalCount(): int
    {
        return $this->getModel()->countAllResults(false);
    }
    //Limit처리
    final public function setLimit(int $per_page): void
    {
        $this->getModel()->limit($per_page);
    }
    //Offset처리
    final public function setOffset(int $offset): void
    {
        $this->getModel()->offset($offset);
    }
    final public function isIPAddress(string $ip, $type = false): bool
    {
        switch ($type) {
            case 'ipv4':
                $result = filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4);
                break;
            case 'ipv6':
                $result = filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6);
                break;
            case 'all':
                $result = filter_var($ip, FILTER_VALIDATE_IP);
                break;
            default:
                $result = filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE);
                break;
        }
        return $result;
    }
    //기본기능
    //개별기능
    //Entity별로 작업처리시
    protected function getEntity_process(mixed $entity): mixed
    {
        return $entity;
    }
    //entities를 가져오는 조건
    protected function getEntities_process(mixed $where = null, array $columns = ['*']): array
    {
        if ($where) {
            $this->getModel()->where($where);
        }
        //출력순서 정의
        $this->setOrderBy();
        $entities = [];
        foreach ($this->getModel()->select(implode(',', $columns))->findAll() as $entity) {
            $entities[$entity->getPK()] = $this->getEntity_process($entity);
        }
        return $entities;
    }
    //FieldForm관련용
    public function getViewFields(): array
    {
        return $this->getFormFields();
    }
    public function getBatchjobFields(): array
    {
        return [];
    }
    public function getBatchjobButtons(): array
    {
        return [
            'batchjob' => '일괄 처리',
            'batchjob_delete' => '일괄 삭제',
        ];
    }
    public function getFormFieldOption(string $field, array $options = []): array
    {
        switch ($field) {
            default:
                $formOptionDatas = lang($this->getClassName() . '.' . strtoupper($field));
                if (!is_array($formOptionDatas)) {
                    throw new \Exception(__FUNCTION__ . "에서 {$field}}의 formOptionDatas 값이 array가 아닙니다.\n" . var_export($formOptionDatas, true));
                }
                $options = [];
                foreach ($formOptionDatas as $key => $value) {
                    $options[$key] = new FormOptionEntity(['uid' => $key, 'title' => $value]);
                }
                break;
        }
        return $options;
    }
    public function getFormFieldRule(string $action, string $field): string
    {
        if (is_array($field)) {
            throw new \Exception(__FUNCTION__ . "=> field가 array 입니다.\n" . var_export($field, true));
        }
        switch ($field) {
            default:
                $rule = $this->getModel()->getFormFieldRule($action, $field);
                break;
        }
        return $rule;
    }
    //생성
    public function create(array $formDatas): mixed
    {
        $entity = $this->getModel()->create($formDatas);
        LogCollector::info("[{$entity->getTitle()}]" . MESSAGES["CREATED"] . ":");
        return $entity;
    }
    //수정
    public function modify(mixed $entity, array $formDatas): mixed
    {
        $entity = $this->getModel()->modify($entity, $formDatas);
        LogCollector::info("[{$entity->getTitle()}]" . MESSAGES["UPDATED"] . ":");
        return $entity;
    }
    ////Index 검색용
    //FormFilter 조건절 처리
    public function index_condition_filterField(string $field, mixed $filter_value): void
    {
        switch ($field) {
            default:
                $this->getModel()->where("{$this->getModel()->getTable()}.{$field}", $filter_value);
                break;
        }
    }
    //검색어조건절처리
    public function index_condition_filterWord(string $word): void
    {
        $this->getModel()->orLike($this->getModel()->getTable() . "." . $this->getModel()->getTitleField(), $word, 'both');
    }
    //날자검색
    public function index_condition_filterDate(string $start, string $end): void
    {
        $this->getModel()->where(sprintf("%s.created_at >= '%s 00:00:00'", $this->getModel()->getTable(), $start));
        $this->getModel()->where(sprintf("%s.created_at <= '%s 23:59:59'", $this->getModel()->getTable(), $end));
    }
    //OrderBy 처리
    public function setOrderBy(mixed $field = null, mixed $value = null): void
    {
        if ($field !== null && $value !== null) {
            $this->getModel()->orderBy(sprintf("%s.%s %s", $this->getModel()->getTable(), $field, $value));
        }
    }
}
