<?php

namespace App\Models;

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

abstract class CommonModel extends Model
{
    protected $table = '';
    protected $primaryKey = '';
    protected $useAutoIncrement = true;
    // protected $returnType = 'array';
    protected $useSoftDeletes = false;
    protected $protectFields = true;
    protected $allowedFields = [];

    protected bool $allowEmptyInserts = false;
    protected bool $updateOnlyChanged = true;

    protected array $casts = [];
    protected array $castHandlers = [];

    // Dates
    protected $useTimestamps = false;
    protected $dateFormat = 'datetime';
    protected $createdField = 'created_at';
    protected $updatedField = 'updated_at';
    protected $deletedField = 'deleted_at';

    // Validation
    protected $validationRules = [];
    protected $validationMessages = [];
    protected $skipValidation = false;
    protected $cleanValidationRules = true;

    // Callbacks
    protected $allowCallbacks = true;
    protected $beforeInsert = [];
    protected $afterInsert = [];
    protected $beforeUpdate = [];
    protected $afterUpdate = [];
    protected $beforeFind = [];
    protected $afterFind = [];
    protected $beforeDelete = [];
    protected $afterDelete = [];

    protected function __construct()
    {
        parent::__construct();
    }
    final public function getTable(): string
    {
        return constant("static::TABLE");
    }
    final public function getPKField(): string
    {
        return constant("static::PK");
    }
    final public function getTitleField(): string
    {
        return constant("static::TITLE");
    }
    //Primary Key로 uuid를 사용시 해당 모델에 아래 변수 반드시 추가 필요
    // protected $useAutoIncrement = false;
    // protected $beforeInsert = ['generateUUID'];
    // allowedFields에는 PK넣으면 않됨, Column Type: CHAR(36)
    // 
    final protected function generateUUID(): string
    {
        $data = random_bytes(16);
        // UUID version 4 (random)
        $data[6] = chr(ord($data[6]) & 0x0f | 0x40); // version 0100
        $data[8] = chr(ord($data[8]) & 0x3f | 0x80); // variant 10
        return vsprintf('%s%s-%s-%s-%s-%s%s%s', str_split(bin2hex($data), 4));
    }
    //기본 기능
    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) {
            case $this->getPKField():
                // 수동입력인 경우
                if (!$this->useAutoIncrement) {
                    $rule = "required|regex_match[/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}/]";
                    $rule .= in_array($action, ["create", "create_form"]) ? "|is_unique[" . $this->getTable() . "." . $field . "]" : "";
                } else {
                    $rule = "required|numeric";
                }
                break;
            case $this->getTitleField():
                $rule = "required|trim|string";
                break;
            case "code":
                // a-zA-Z → 영문 대소문자,0-9 → 숫자,가-힣 → 한글 완성형,\- → 하이픈
                $rule = "required|regex_match[/^[a-zA-Z0-9가-힣\-]+$/]|min_length[4]|max_length[20]";
                $rule .= in_array($action, ["create", "create_form"]) ? "|is_unique[" . $this->getTable() . "." . $field . "]" : "";
                break;
            case 'picture':
                $rule = "is_image[{$field}]|mime_in[{$field},image/jpg,image/jpeg,image/gif,image/png,image/webp]|max_size[{$field},300]|max_dims[{$field},2048,768]";
                break;
            case "updated_at":
            case "created_at":
            case "deleted_at":
                $rule = "permit_empty|valid_date";
                break;
            default:
                $rule = "permit_empty|trim|string";
                break;
        }
        return $rule;
    }
    // create, modify 직전 작업용 작업
    protected function convertFormDatas(string $action, string $field, array $formDatas): mixed
    {
        // 필드 값 존재 여부 확인
        $value = array_key_exists($field, $formDatas) ? $formDatas[$field] : null;
        switch ($field) {
            case $this->getPKField():
                // 수동입력인 경우
                if (!$this->useAutoIncrement) {
                    $randomBytes = bin2hex(random_bytes(32));
                    $value       = sprintf(
                        '%08s-%04s-%04x-%04x-%12s',
                        substr($randomBytes, 0, 8),
                        substr($randomBytes, 8, 4),
                        substr($randomBytes, 12, 4),
                        substr($randomBytes, 16, 4),
                        substr($randomBytes, 20, 12)
                    );
                }
                break;
            case "editor":
            case "detail":
            case "content":
            case "discription":
            case "history":
                // textarea 계열은 XSS 방지
                if ($value === '' || $value === null) {
                    //값이 없거나 빈데이터면 아무것도 하지 않음
                } elseif ($value === ' ') {
                    $value = '';   // 공백 하나를 넣어서 의도적인 빈칸 저장
                } else {
                    $value = htmlentities($value, ENT_QUOTES, 'UTF-8');
                }
                break;
        }
        return $value;
    }
    public function create(array $formDatas): mixed
    {
        // LogCollector::debug("입력내용");
        // LogCollector::debug(var_export($formDatas, true));
        $convertedFormDatas = [];
        foreach ($this->allowedFields as $field) {
            $value = $this->convertFormDatas(
                __FUNCTION__,
                $field,
                $formDatas
            );
            if ($value !== null) {
                $convertedFormDatas[$field] = $value;
            }
        }
        // 최종 저장 시 오류 발생하면
        if (!$this->save($convertedFormDatas)) {
            $message = sprintf(
                "\n------%s 오류-----\n%s\n%s\n------------------------------\n",
                __METHOD__,
                var_export($this->errors(), true),
                $this->getLastQuery()
            );
            LogCollector::debug($message);
            throw new \Exception($message);
        }
        //Model별 returntype형의 Entity 호출
        if (!class_exists($this->returnType)) {
            throw new \RuntimeException(__METHOD__ . "에서 returnType: {$this->returnType}이 정의되지 않았습니다.");
        }
        $entity = new $this->returnType($convertedFormDatas);
        // primaryKey가 자동입력이면
        if ($this->useAutoIncrement) {
            $pkField = $this->getPKField();
            $entity->$pkField = $this->getInsertID();
        }
        return $entity;
    }
    public function modify(mixed $entity, array $formDatas): mixed
    {
        // 저장하기 전에 데이터 값 변경이 필요한 Field
        // LogCollector::debug("[{$entity->getPK()}/{$entity->getTitle()}] 변경 전 내용");
        // LogCollector::debug(var_export($formDatas, true));
        // LogCollector::debug(var_export($entity->toArray(), true));

        foreach ($this->allowedFields as $field) {
            $value = $this->convertFormDatas(
                __FUNCTION__,
                $field,
                $formDatas
            );
            if ($value !== null) {
                $entity->$field = $value;
            }
        }
        //수정일추가
        $entity->setUpdatedAt(date("Y-m-d H:i:s"));
        // 최종 저장 시 오류 발생하면
        if (!$this->save($entity)) {
            $message = sprintf(
                "\n------%s 오류-----\n%s\n------------------------------\n",
                __METHOD__,
                var_export($this->errors(), true)
            );
            LogCollector::debug($message);
            throw new \Exception($message);
        }
        return $entity;
    }
}
