<?php

namespace App\Models;

use App\Entities\BoardEntity;

class BoardModel extends BaseHierarchyModel
{
    private $_boardconfig_options = null;
    protected $table = "tw_board";
    protected $returnType = BoardEntity::class;
    public function __construct()
    {
        parent::__construct('Board');
        $this->allowedFields = [
            ...$this->allowedFields,
            "board_config_uid",
            "user_uid", 'title', "content",
            "passwd", "board_file", "view_cnt", "status"
        ];
        $this->validationRules = [...$this->validationRules, ...$this->getFieldRules($this->allowedFields),];
    }
    public function getTitleField(): string
    {
        return 'title';
    }
    public function getContentField(): string
    {
        return 'content';
    }
    protected function getFieldRule(string $field, array $rules, string $action = ""): array
    {
        switch ($field) {
            case "board_config_uid":
                $rules[$field] =  "required|regex_match[/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}/]";
                break;
            case $this->getTitleField():
            case "content":
                $rules[$field] =  "required|string";
                break;
            case "board_file": //uploaded[{$field}] == requried와 같은의미
                $rules[$field] = !$action ? "if_exist|string" : "is_image[{$field}]|mime_in[{$field},image/jpg,image/jpeg,image/gif,image/png,image/webp]|max_size[{$field},100]|max_dims[{$field},1024,768]";
                break;
            case "view_cnt":
                $rules[$field] =  "if_exist|numeric";
                break;
            default:
                $rules = parent::getFieldRule($field, $rules, $action);
                break;
        }
        return $rules;
    }
    //Field별 Form Option용
    public function getFieldFormOption(string $field): array
    {
        switch ($field) {
            case 'board_config_uid':
                if (is_null($this->_boardconfig_options)) {
                    $boardConfigModel = new BoardConfigModel();
                    $this->_boardconfig_options = $boardConfigModel->getOptions();
                }
                $options = $this->_boardconfig_options;
                break;
            default:
                return parent::getFieldFormOption($field);
                break;
        }
        if (!is_array($options)) {
            throw new \Exception(__FUNCTION__ . "에서 {$this->getClassName()}의 Field:{$field}의 FormOptionData가 array가 아닙니다.\n" . var_export($options, true));
        }
        return $options;
    }
    public function getEntity($conditions): BoardEntity
    {
        return $this->where($conditions)->first() ?: throw new \Exception("해당 데이터가 없습니다.\n" . var_export($conditions, true));
    }

    public function create(array $formDatas): BoardEntity
    {
        return $this->create_process(new BoardEntity(), $formDatas);
    }
    public function modify(BoardEntity $entity, array $formDatas): BoardEntity
    {
        return $this->modify_process($entity, $formDatas);
    }
    public function reply($parent_entity, array $formDatas): BoardEntity
    {
        return $this->reply_process($parent_entity, new BoardEntity(), $formDatas);
    }

    //Index관련
    public function setIndexWordFilter(string $word)
    {
        parent::setIndexWordFilter($word);
        $this->orLike($this->getTitleField(), $word, "both");
        $this->orLike("content", $word, "both"); //befor , after , both
    }
}
